コード例 #1
0
  /**
   * @param channel
   * @param eventListener
   * @return
   * @throws IOException
   * @throws SshException
   */
  public synchronized boolean openChannel(Channel channel, ChannelEventListener eventListener)
      throws IOException {
    synchronized (activeChannels) {
      Long channelId = getChannelId();

      // Create the message
      SshMsgChannelOpen msg =
          new SshMsgChannelOpen(
              channel.getChannelType(),
              channelId.longValue(),
              channel.getLocalWindow().getWindowSpace(),
              channel.getLocalPacketSize(),
              channel.getChannelOpenData());

      // Send the message
      transport.sendMessage(msg, this);

      // Wait for the next message to confirm the open channel (or not)
      int[] messageIdFilter = new int[2];
      messageIdFilter[0] = SshMsgChannelOpenConfirmation.SSH_MSG_CHANNEL_OPEN_CONFIRMATION;
      messageIdFilter[1] = SshMsgChannelOpenFailure.SSH_MSG_CHANNEL_OPEN_FAILURE;

      try {
        SshMessage result = messageStore.getMessage(messageIdFilter);

        if (result.getMessageId()
            == SshMsgChannelOpenConfirmation.SSH_MSG_CHANNEL_OPEN_CONFIRMATION) {
          SshMsgChannelOpenConfirmation conf = (SshMsgChannelOpenConfirmation) result;
          activeChannels.put(channelId, channel);
          log.debug("Initiating channel");
          channel.init(
              this,
              channelId.longValue(),
              conf.getSenderChannel(),
              conf.getInitialWindowSize(),
              conf.getMaximumPacketSize(),
              eventListener);
          channel.open();
          log.info(
              "Channel "
                  + String.valueOf(channel.getLocalChannelId())
                  + " is open ["
                  + channel.getName()
                  + "]");

          return true;
        } else {
          // Make sure the channels state is closed
          channel.getState().setValue(ChannelState.CHANNEL_CLOSED);

          return false;
        }
      } catch (MessageStoreEOFException mse) {
        throw new IOException(mse.getMessage());
      } catch (InterruptedException ex) {
        throw new SshException(
            "The thread was interrupted whilst waiting for a connection protocol message");
      }
    }
  }
コード例 #2
0
  /**
   * @param channel
   * @param requestType
   * @param wantReply
   * @param requestData
   * @return
   * @throws IOException
   * @throws SshException
   */
  public synchronized boolean sendChannelRequest(
      Channel channel, String requestType, boolean wantReply, byte[] requestData)
      throws IOException {
    boolean success = true;
    log.debug(
        "Sending " + requestType + " request for the " + channel.getChannelType() + " channel");

    SshMsgChannelRequest msg =
        new SshMsgChannelRequest(channel.getRemoteChannelId(), requestType, wantReply, requestData);
    transport.sendMessage(msg, this);

    // If the user requests a reply then wait for the message and return result
    if (wantReply) {
      // Set up our message filter
      int[] messageIdFilter = new int[2];
      messageIdFilter[0] = SshMsgChannelSuccess.SSH_MSG_CHANNEL_SUCCESS;
      messageIdFilter[1] = SshMsgChannelFailure.SSH_MSG_CHANNEL_FAILURE;
      log.debug("Waiting for channel request reply");

      try {
        // Wait for either success or failure
        SshMessage reply = messageStore.getMessage(messageIdFilter);

        switch (reply.getMessageId()) {
          case SshMsgChannelSuccess.SSH_MSG_CHANNEL_SUCCESS:
            {
              log.debug("Channel request succeeded");
              success = true;

              break;
            }

          case SshMsgChannelFailure.SSH_MSG_CHANNEL_FAILURE:
            {
              log.debug("Channel request failed");
              success = false;

              break;
            }
        }
      } catch (InterruptedException ex) {
        throw new SshException(
            "The thread was interrupted whilst waiting for a connection protocol message");
      }
    }

    return success;
  }
コード例 #3
0
  /**
   * @param requestName
   * @param wantReply
   * @param requestData
   * @return
   * @throws IOException
   * @throws SshException
   */
  public synchronized byte[] sendGlobalRequest(
      String requestName, boolean wantReply, byte[] requestData) throws IOException {
    boolean success = true;
    SshMsgGlobalRequest msg = new SshMsgGlobalRequest(requestName, true, requestData);
    transport.sendMessage(msg, this);

    if (wantReply) {
      // Set up our message filter
      int[] messageIdFilter = new int[2];
      messageIdFilter[0] = SshMsgRequestSuccess.SSH_MSG_REQUEST_SUCCESS;
      messageIdFilter[1] = SshMsgRequestFailure.SSH_MSG_REQUEST_FAILURE;
      log.debug("Waiting for global request reply");

      try {
        // Wait for either success or failure
        SshMessage reply = messageStore.getMessage(messageIdFilter);

        switch (reply.getMessageId()) {
          case SshMsgRequestSuccess.SSH_MSG_REQUEST_SUCCESS:
            {
              log.debug("Global request succeeded");

              return ((SshMsgRequestSuccess) reply).getRequestData();
            }

          case SshMsgRequestFailure.SSH_MSG_REQUEST_FAILURE:
            {
              log.debug("Global request failed");
              throw new SshException("The request failed");
            }
        }
      } catch (InterruptedException ex) {
        throw new SshException(
            "The thread was interrupted whilst waiting for a connection protocol message");
      }
    }

    return null;
  }
コード例 #4
0
  /**
   * @param msg
   * @throws IOException
   */
  protected void onMessageReceived(SshMessage msg) throws IOException {
    // Route the message to the correct handling function
    switch (msg.getMessageId()) {
      case SshMsgGlobalRequest.SSH_MSG_GLOBAL_REQUEST:
        {
          onMsgGlobalRequest((SshMsgGlobalRequest) msg);

          break;
        }

      case SshMsgChannelOpen.SSH_MSG_CHANNEL_OPEN:
        {
          onMsgChannelOpen((SshMsgChannelOpen) msg);

          break;
        }

      case SshMsgChannelClose.SSH_MSG_CHANNEL_CLOSE:
        {
          onMsgChannelClose((SshMsgChannelClose) msg);

          break;
        }

      case SshMsgChannelEOF.SSH_MSG_CHANNEL_EOF:
        {
          onMsgChannelEOF((SshMsgChannelEOF) msg);

          break;
        }

      case SshMsgChannelData.SSH_MSG_CHANNEL_DATA:
        {
          onMsgChannelData((SshMsgChannelData) msg);

          break;
        }

      case SshMsgChannelExtendedData.SSH_MSG_CHANNEL_EXTENDED_DATA:
        {
          onMsgChannelExtendedData((SshMsgChannelExtendedData) msg);

          break;
        }

      case SshMsgChannelRequest.SSH_MSG_CHANNEL_REQUEST:
        {
          onMsgChannelRequest((SshMsgChannelRequest) msg);

          break;
        }

      case SshMsgChannelWindowAdjust.SSH_MSG_CHANNEL_WINDOW_ADJUST:
        {
          onMsgChannelWindowAdjust((SshMsgChannelWindowAdjust) msg);

          break;
        }

      default:
        {
          // If we never registered it why are we getting it?
          log.debug("Message not handled");
          throw new IOException("Unregistered message received!");
        }
    }
  }