/**
  * Sends a specific <tt>RawPacket</tt> through this <tt>OutputDataStream</tt> to a specific
  * <tt>InetSocketAddress</tt>.
  *
  * @param packet the <tt>RawPacket</tt> to send through this <tt>OutputDataStream</tt> to the
  *     specified <tt>target</tt>
  * @param target the <tt>InetSocketAddress</tt> to which the specified <tt>packet</tt> is to be
  *     sent through this <tt>OutputDataStream</tt>
  * @throws IOException if anything goes wrong while sending the specified <tt>packet</tt> through
  *     this <tt>OutputDataStream</tt> to the specified <tt>target</tt>
  */
 @Override
 protected void sendToTarget(RawPacket packet, InetSocketAddress target) throws IOException {
   socket.send(
       new DatagramPacket(
           packet.getBuffer(),
           packet.getOffset(),
           packet.getLength(),
           target.getAddress(),
           target.getPort()));
 }
  /**
   * Log the packet.
   *
   * @param packet packet to log
   */
  @Override
  protected void doLogPacket(RawPacket packet, InetSocketAddress target) {
    if (socket == null || packet == null || target == null) return;

    // Do not log the packet if it has been processed (and already
    // logged) by the ice4j stack.
    if (socket instanceof MultiplexingDatagramSocket) return;

    PacketLoggingService pktLogging = getPacketLoggingService();

    if (pktLogging != null) {
      pktLogging.logPacket(
          PacketLoggingService.ProtocolName.RTP,
          socket.getLocalAddress().getAddress(),
          socket.getLocalPort(),
          target.getAddress().getAddress(),
          target.getPort(),
          PacketLoggingService.TransportName.UDP,
          true,
          packet.getBuffer(),
          packet.getOffset(),
          packet.getLength());
    }
  }
Esempio n. 3
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);
    }
  }