Apache Mina и SSHD (обратный SSH): неверная длина пакета 0

Пожалуйста, поделитесь своими идеями о том, как мы можем разрешить это исключение

Мы работаем над Netconf Over SSH, а используемая библиотека SSH - это Apache Mina и SSHD. Устройство - это сервер Netconf, а приложение - это клиент Netconf. Поскольку устройство находится за брандмауэром, приложение не может подключиться к устройству, но устройство может подключиться к приложению. Итак, мы используем сеанс, установленный устройством для приложения для всей связи.

Ниже приведены следующие шаги:
1. Запустил NioSocketAcceptor прослушивание / привязку к номеру порта
2. Атрибут useReadoperation IoSessionConfig имеет значение true
3. Когда принято tcp-соединение, мы имеем доступную минимальную IoSession, здесь мы называем ее tcpSession.
4. Мы хотели использовать эту минимальную IoSession для чтения и записи сообщений netconf. Код, используемый для преобразования ядра mina в ssdh IoSession, выглядит следующим образом

new org.apache.sshd.common.io.IoSession() {         

        @Override
        public long getId() {
            return tcpSession.getId();
        }

        @Override
        public Object getAttribute(final Object key) {
            return tcpSession.getAttribute(key);
        }

        @Override
        public Object setAttribute(final Object key, final Object value) {
            return tcpSession.getAttribute(key, value);
        }

        @Override
        public SocketAddress getRemoteAddress() {
            return tcpSession.getRemoteAddress();
        }

        @Override
        public SocketAddress getLocalAddress() {
            return tcpSession.getLocalAddress();
        }

        @Override
        public IoWriteFuture write(final Buffer buffer) {
            final ChannelAsyncOutputStream.IoWriteFutureImpl ioWriteFuture = new ChannelAsyncOutputStream.IoWriteFutureImpl(buffer);
            byte[] bytes = buffer.getCompactData();   
            int capacity = bytes.length + 8;   
            IoBuffer bytebuffer = IoBuffer.allocate(capacity).setAutoExpand(true);   
            //bytebuffer.putInt(bytes.length);   
            bytebuffer.put(bytes);   
            bytebuffer.flip();              
            buffer.clear();
            final WriteFuture write = tcpSession.write(bytebuffer);
            write.addListener(new IoFutureListener<WriteFuture>() {
                @Override
                public void operationComplete(final WriteFuture future) {
                    if (future.isWritten()) {
                        ioWriteFuture.setValue(true);
                    } else {
                        // TODO check the value type expected + if exception can go there
                        ioWriteFuture.setValue(future.getException());
                    }
                }
            });

            return ioWriteFuture;
        }       

        @Override
        public CloseFuture close(final boolean immediately) {
            final DefaultCloseFuture defaultCloseFuture = new DefaultCloseFuture(null);
            tcpSession.close(immediately).addListener(new IoFutureListener<org.apache.mina.core.future.CloseFuture>() {
                @Override
                public void operationComplete(final org.apache.mina.core.future.CloseFuture future) {
                    if(future.isClosed()) {
                        defaultCloseFuture.setValue(true);
                    } else {
                        // TODO check the value type expected
                        defaultCloseFuture.setValue(false);
                    }
                }
            });
            return defaultCloseFuture;
        }

        @Override
        public IoService getService() {
            throw new UnsupportedOperationException("No service available");
        }

        @Override
        public boolean isClosed() {
            return tcpSession.getCloseFuture().isClosed();
        }

        @Override
        public boolean isClosing() {
            return tcpSession.isClosing();
        }
    });

5. Код, используемый для присоединения Apache SSHD IoSession к клиенту, приведен ниже.

final ConnectFuture connectFuture = new DefaultConnectFuture(null);
    ClientSessionImpl session = null;
    try {
        session = new ClientSessionImpl(sshClient, ioSession);
        AbstractSession.attachSession(ioSession, session);
        MyAsyncSshHandlerReader async = new MyAsyncSshHandlerReader(ioSession);
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
  1. MyAsyncSshHandlerReader - это класс, используемый для чтения данных из tcpSession.

    Класс MyAsyncSshHandlerReader реализует IoFutureListener, AutoCloseable {

    private final org.apache.sshd.common.io.IoSession ioSession;
    
    public MyAsyncSshHandlerReader(org.apache.sshd.common.io.IoSession ioSession) {
        this.ioSession = ioSession;
        minaCoreIoSession.read().addListener(this);
    }
    
    @Override
    public void close() throws Exception {
        // TODO Auto-generated method stub
    
    }
    
    @Override
    public void operationComplete(ReadFuture future) {
        long msgsRead = future.getSession().getReadMessages();
        if(future.isRead() && !future.isClosed()){
    
            IoBuffer msg = (IoBuffer)future.getMessage();               
            msg.flip();
            Buffer buf = new Buffer(msg.array());           
            try {               
                if(AbstractSession.getSession(ioSession) != null){
                    AbstractSession.getSession(ioSession).messageReceived(buf);                 
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            minaCoreIoSession.read().addListener(this);
        }           
    }
    

    }

  2. Когда сеанс клиента создается на стороне приложения, нижеприведенные методы вызываются.

    1. sendIdentification
    2. sendKexInit ()
  3. Приложение также получило идентификационное сообщение и ключ, обмененный с устройством, и идентификационное сообщение прочитано правильно, но не удалось декодировать сообщение обмена ключами, библиотека вызвала SshException "недопустимая длина пакета 0"

2015-11-02 13:02: 48,831 | ИНФО | oupCloseable-6-2 | ClientSessionImpl | 210 - org.apache.sshd.core - 0.14.0 | Пакет с декодированием ошибок (недопустимая длина) 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 05 фк 07 14 34 d4 4e 0e 2c 22 73 e4 7e 46 41 1b e3 55 27 ef 00 00 00 b7 65 63 64 68 2d 73 68 61 32 2d 6e 69 73 74 70 32 35 36 2c 65 63 64 68 2d 73 68 61 32 2d 6e 69 73 74 70 33 38 34 2c 65 63 64 68 2d 73 68 61 32 2d 6e 69 73 74 70 35 32 31 2c 64 69 66 66 69 65 2d 68 65 6c 6c 6d 61 6e 2d 67 72 6f 75 70 2d 65 78 63 68 61 6e 67 65 2d 73 68 61 32 35 36 2c 64 69 66 66 69 65 2d 68 65 6c 6c 6d 61 6e 2d 67 72 6f 75 70 2d 65 78 63 68 61 6e 67 65 2d 73 68 61 31 2c 64 69 66 66 69 65 2d 68 65 6c 6c 6d 61 6e 2d 67 72 6f 75 70 31 34 2d 73 68 61 31 2c 64 69 66org.apache.sshd.common.SshException: недопустимая длина пакета:0 в org.apache.sshd.common.session.AbstractSession.decode(AbstractSession.java:719) в org.apache.sshd.common.session.AbstractSession.messageReceived(AbstractSession.java:308) в org.opendaylight.controller.netconf.nettyutil.handler.ssh.client.ReversedAsyncSshHandler$MyAsyncSshHandlerReader.operationComplete(ReversedAsyncSshHandler.java:220)

  1. пожалуйста, расскажите, как работает метод AbstractSession.decode().

Из моей отладки переменные rpos и limit содержат одно и то же значение, например, 1536. У способа, которым я читаю, есть некоторая проблема?

заранее спасибо

1 ответ

Изучив много примеров и библиотеку Apache mina, я обнаружил, что проблема заключается в том, как мы читаем из mina core IoSession.

Правильный способ чтения::

int r=ioBuffer.remaining();
byte[] b=new byte[r];
ioBuffer.get(b,0,r);

Buffer buf = new Buffer(b);
AbstractSession.getSession(tcpSession).messageReceived(buf)
Другие вопросы по тегам