Пример #1
0
  /**
   * Logs the specified message and details to the packet logging service if enabled.
   *
   * @param message the message to log
   * @param sender determines whether we are the origin of this message.
   */
  private void logPacket(SIPMessage message, boolean sender) {
    try {
      PacketLoggingService packetLogging = SipActivator.getPacketLogging();
      if (packetLogging == null
          || !packetLogging.isLoggingEnabled(PacketLoggingService.ProtocolName.SIP)
          /* Via not present in CRLF packet on TCP - causes NPE */
          || message.getTopmostVia() == null) return;

      String transport = message.getTopmostVia().getTransport();
      boolean isTransportUDP = transport.equalsIgnoreCase("UDP");

      byte[] srcAddr;
      int srcPort;
      byte[] dstAddr;
      int dstPort;

      // if addresses are not set use empty byte array with length
      // equals to the other address or just empty
      // byte array with length 4 (ipv4 0.0.0.0)
      if (sender) {
        if (!isTransportUDP) {
          InetSocketAddress localAddress =
              getLocalAddressForDestination(
                  message.getRemoteAddress(),
                  message.getRemotePort(),
                  message.getLocalAddress(),
                  transport);
          srcPort = localAddress.getPort();
          srcAddr = localAddress.getAddress().getAddress();
        } else {
          srcPort = message.getLocalPort();
          if (message.getLocalAddress() != null) srcAddr = message.getLocalAddress().getAddress();
          else if (message.getRemoteAddress() != null)
            srcAddr = new byte[message.getRemoteAddress().getAddress().length];
          else srcAddr = new byte[4];
        }

        dstPort = message.getRemotePort();
        if (message.getRemoteAddress() != null) dstAddr = message.getRemoteAddress().getAddress();
        else dstAddr = new byte[srcAddr.length];
      } else {
        if (!isTransportUDP) {
          InetSocketAddress dstAddress =
              getLocalAddressForDestination(
                  message.getRemoteAddress(),
                  message.getRemotePort(),
                  message.getLocalAddress(),
                  transport);
          dstPort = dstAddress.getPort();
          dstAddr = dstAddress.getAddress().getAddress();
        } else {
          dstPort = message.getLocalPort();
          if (message.getLocalAddress() != null) dstAddr = message.getLocalAddress().getAddress();
          else if (message.getRemoteAddress() != null)
            dstAddr = new byte[message.getRemoteAddress().getAddress().length];
          else dstAddr = new byte[4];
        }

        srcPort = message.getRemotePort();
        if (message.getRemoteAddress() != null) srcAddr = message.getRemoteAddress().getAddress();
        else srcAddr = new byte[dstAddr.length];
      }

      byte[] msg = null;
      if (message instanceof SIPRequest) {
        SIPRequest req = (SIPRequest) message;
        if (req.getMethod().equals(SIPRequest.MESSAGE)
            && message.getContentTypeHeader() != null
            && message.getContentTypeHeader().getContentType().equalsIgnoreCase("text")) {
          int len = req.getContentLength().getContentLength();

          if (len > 0) {
            SIPRequest newReq = (SIPRequest) req.clone();

            byte[] newContent = new byte[len];
            Arrays.fill(newContent, (byte) '.');
            newReq.setMessageContent(newContent);
            msg = newReq.toString().getBytes("UTF-8");
          }
        }
      }

      if (msg == null) {
        msg = message.toString().getBytes("UTF-8");
      }

      packetLogging.logPacket(
          PacketLoggingService.ProtocolName.SIP,
          srcAddr,
          srcPort,
          dstAddr,
          dstPort,
          isTransportUDP
              ? PacketLoggingService.TransportName.UDP
              : PacketLoggingService.TransportName.TCP,
          sender,
          msg);
    } catch (Throwable e) {
      logger.error("Cannot obtain message body", e);
    }
  }