Search in sources :

Example 6 with TypesReader

use of com.trilead.ssh2.packets.TypesReader in project intellij-community by JetBrains.

the class SFTPv3Client method expectStatusOKMessage.

private void expectStatusOKMessage(int id) throws IOException {
    byte[] resp = receiveMessage(34000);
    if (debug != null) {
        debug.println("Got REPLY.");
        debug.flush();
    }
    TypesReader tr = new TypesReader(resp);
    int t = tr.readByte();
    int rep_id = tr.readUINT32();
    if (rep_id != id)
        throw new IOException("The server sent an invalid id field.");
    if (t != Packet.SSH_FXP_STATUS)
        throw new IOException("The SFTP server sent an unexpected packet type (" + t + ")");
    int errorCode = tr.readUINT32();
    if (errorCode == ErrorCodes.SSH_FX_OK)
        return;
    throw new SFTPException(tr.readString(), errorCode);
}
Also used : TypesReader(com.trilead.ssh2.packets.TypesReader)

Example 7 with TypesReader

use of com.trilead.ssh2.packets.TypesReader in project intellij-community by JetBrains.

the class SFTPv3Client method scanDirectory.

private final Vector scanDirectory(byte[] handle) throws IOException {
    Vector files = new Vector();
    while (true) {
        int req_id = generateNextRequestID();
        TypesWriter tw = new TypesWriter();
        tw.writeString(handle, 0, handle.length);
        if (debug != null) {
            debug.println("Sending SSH_FXP_READDIR...");
            debug.flush();
        }
        sendMessage(Packet.SSH_FXP_READDIR, req_id, tw.getBytes());
        /* Some servers send here a packet with size > 34000 */
        /* To whom it may concern: please learn to read the specs. */
        byte[] resp = receiveMessage(65536);
        if (debug != null) {
            debug.println("Got REPLY.");
            debug.flush();
        }
        TypesReader tr = new TypesReader(resp);
        int t = tr.readByte();
        int rep_id = tr.readUINT32();
        if (rep_id != req_id)
            throw new IOException("The server sent an invalid id field.");
        if (t == Packet.SSH_FXP_NAME) {
            int count = tr.readUINT32();
            if (debug != null)
                debug.println("Parsing " + count + " name entries...");
            while (count > 0) {
                SFTPv3DirectoryEntry dirEnt = new SFTPv3DirectoryEntry();
                dirEnt.filename = tr.readString(charsetName);
                dirEnt.longEntry = tr.readString(charsetName);
                dirEnt.attributes = readAttrs(tr);
                files.addElement(dirEnt);
                if (debug != null)
                    debug.println("File: '" + dirEnt.filename + "'");
                count--;
            }
            continue;
        }
        if (t != Packet.SSH_FXP_STATUS)
            throw new IOException("The SFTP server sent an unexpected packet type (" + t + ")");
        int errorCode = tr.readUINT32();
        if (errorCode == ErrorCodes.SSH_FX_EOF)
            return files;
        throw new SFTPException(tr.readString(), errorCode);
    }
}
Also used : TypesReader(com.trilead.ssh2.packets.TypesReader) Vector(java.util.Vector) TypesWriter(com.trilead.ssh2.packets.TypesWriter)

Example 8 with TypesReader

use of com.trilead.ssh2.packets.TypesReader in project intellij-community by JetBrains.

the class TransportManager method receiveLoop.

public void receiveLoop() throws IOException {
    byte[] msg = new byte[35000];
    while (true) {
        int msglen = tc.receiveMessage(msg, 0, msg.length);
        int type = msg[0] & 0xff;
        if (type == Packets.SSH_MSG_IGNORE)
            continue;
        if (type == Packets.SSH_MSG_DEBUG) {
            if (log.isEnabled()) {
                TypesReader tr = new TypesReader(msg, 0, msglen);
                tr.readByte();
                tr.readBoolean();
                StringBuffer debugMessageBuffer = new StringBuffer();
                debugMessageBuffer.append(tr.readString("UTF-8"));
                for (int i = 0; i < debugMessageBuffer.length(); i++) {
                    char c = debugMessageBuffer.charAt(i);
                    if ((c >= 32) && (c <= 126))
                        continue;
                    debugMessageBuffer.setCharAt(i, '�');
                }
                log.log(50, "DEBUG Message from remote: '" + debugMessageBuffer.toString() + "'");
            }
            continue;
        }
        if (type == Packets.SSH_MSG_UNIMPLEMENTED) {
            throw new IOException("Peer sent UNIMPLEMENTED message, that should not happen.");
        }
        if (type == Packets.SSH_MSG_DISCONNECT) {
            TypesReader tr = new TypesReader(msg, 0, msglen);
            tr.readByte();
            int reason_code = tr.readUINT32();
            StringBuffer reasonBuffer = new StringBuffer();
            reasonBuffer.append(tr.readString("UTF-8"));
            if (reasonBuffer.length() > 255) {
                reasonBuffer.setLength(255);
                reasonBuffer.setCharAt(254, '.');
                reasonBuffer.setCharAt(253, '.');
                reasonBuffer.setCharAt(252, '.');
            }
            for (int i = 0; i < reasonBuffer.length(); i++) {
                char c = reasonBuffer.charAt(i);
                if ((c >= 32) && (c <= 126))
                    continue;
                reasonBuffer.setCharAt(i, '�');
            }
            throw new IOException("Peer sent DISCONNECT message (reason code " + reason_code + "): " + reasonBuffer.toString());
        }
        if ((type == Packets.SSH_MSG_KEXINIT) || (type == Packets.SSH_MSG_NEWKEYS) || ((type >= 30) && (type <= 49))) {
            km.handleMessage(msg, msglen);
            continue;
        }
        MessageHandler mh = null;
        for (int i = 0; i < messageHandlers.size(); i++) {
            HandlerEntry he = (HandlerEntry) messageHandlers.elementAt(i);
            if ((he.low <= type) && (type <= he.high)) {
                mh = he.mh;
                break;
            }
        }
        if (mh == null)
            throw new IOException("Unexpected SSH message (type " + type + ")");
        mh.handleMessage(msg, msglen);
    }
}
Also used : TypesReader(com.trilead.ssh2.packets.TypesReader) IOException(java.io.IOException)

Example 9 with TypesReader

use of com.trilead.ssh2.packets.TypesReader in project intellij-community by JetBrains.

the class SFTPv3Client method statBoth.

private SFTPv3FileAttributes statBoth(String path, int statMethod) throws IOException {
    int req_id = generateNextRequestID();
    TypesWriter tw = new TypesWriter();
    tw.writeString(path, charsetName);
    if (debug != null) {
        debug.println("Sending SSH_FXP_STAT/SSH_FXP_LSTAT...");
        debug.flush();
    }
    sendMessage(statMethod, req_id, tw.getBytes());
    byte[] resp = receiveMessage(34000);
    if (debug != null) {
        debug.println("Got REPLY.");
        debug.flush();
    }
    TypesReader tr = new TypesReader(resp);
    int t = tr.readByte();
    int rep_id = tr.readUINT32();
    if (rep_id != req_id)
        throw new IOException("The server sent an invalid id field.");
    if (t == Packet.SSH_FXP_ATTRS) {
        return readAttrs(tr);
    }
    if (t != Packet.SSH_FXP_STATUS)
        throw new IOException("The SFTP server sent an unexpected packet type (" + t + ")");
    int errorCode = tr.readUINT32();
    throw new SFTPException(tr.readString(), errorCode);
}
Also used : TypesReader(com.trilead.ssh2.packets.TypesReader) TypesWriter(com.trilead.ssh2.packets.TypesWriter)

Example 10 with TypesReader

use of com.trilead.ssh2.packets.TypesReader in project intellij-community by JetBrains.

the class SFTPv3Client method write.

/**
	 * Write bytes to a file. If <code>len</code> &gt; 32768, then the write operation will
	 * be split into multiple writes.
	 * 
	 * @param handle a SFTPv3FileHandle handle.
	 * @param fileOffset offset (in bytes) in the file.
	 * @param src the source byte array.
	 * @param srcoff offset in the source byte array.
	 * @param len how many bytes to write.
	 * @throws IOException
	 */
public void write(SFTPv3FileHandle handle, long fileOffset, byte[] src, int srcoff, int len) throws IOException {
    checkHandleValidAndOpen(handle);
    while (len > 0) {
        int writeRequestLen = len;
        if (writeRequestLen > 32768)
            writeRequestLen = 32768;
        int req_id = generateNextRequestID();
        TypesWriter tw = new TypesWriter();
        tw.writeString(handle.fileHandle, 0, handle.fileHandle.length);
        tw.writeUINT64(fileOffset);
        tw.writeString(src, srcoff, writeRequestLen);
        if (debug != null) {
            debug.println("Sending SSH_FXP_WRITE...");
            debug.flush();
        }
        sendMessage(Packet.SSH_FXP_WRITE, req_id, tw.getBytes());
        fileOffset += writeRequestLen;
        srcoff += writeRequestLen;
        len -= writeRequestLen;
        byte[] resp = receiveMessage(34000);
        TypesReader tr = new TypesReader(resp);
        int t = tr.readByte();
        int rep_id = tr.readUINT32();
        if (rep_id != req_id)
            throw new IOException("The server sent an invalid id field.");
        if (t != Packet.SSH_FXP_STATUS)
            throw new IOException("The SFTP server sent an unexpected packet type (" + t + ")");
        int errorCode = tr.readUINT32();
        if (errorCode == ErrorCodes.SSH_FX_OK)
            continue;
        String errorMessage = tr.readString();
        throw new SFTPException(errorMessage, errorCode);
    }
}
Also used : TypesReader(com.trilead.ssh2.packets.TypesReader) TypesWriter(com.trilead.ssh2.packets.TypesWriter)

Aggregations

TypesReader (com.trilead.ssh2.packets.TypesReader)16 TypesWriter (com.trilead.ssh2.packets.TypesWriter)10 IOException (java.io.IOException)5 BigInteger (java.math.BigInteger)4 Vector (java.util.Vector)1