Subversion Repositories Programming Utils

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
86 rm5248 1
/*
2
 * Licensed to the Apache Software Foundation (ASF) under one
3
 * or more contributor license agreements.  See the NOTICE file
4
 * distributed with this work for additional information
5
 * regarding copyright ownership.  The ASF licenses this file
6
 * to you under the Apache License, Version 2.0 (the
7
 * "License"); you may not use this file except in compliance
8
 * with the License.  You may obtain a copy of the License at
9
 *
10
 *   http://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 * Unless required by applicable law or agreed to in writing,
13
 * software distributed under the License is distributed on an
14
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15
 * KIND, either express or implied.  See the License for the
16
 * specific language governing permissions and limitations
17
 * under the License.
18
 */
19
package org.apache.sshd.agent.unix;
20
 
21
import java.io.IOException;
22
 
23
import org.apache.sshd.client.channel.AbstractClientChannel;
24
import org.apache.sshd.common.SshConstants;
25
import org.apache.sshd.common.channel.ChannelOutputStream;
26
import org.apache.tomcat.jni.Socket;
27
import org.apache.tomcat.jni.Status;
28
 
29
public class AgentForwardedChannel extends AbstractClientChannel implements Runnable {
30
 
31
    private final long socket;
32
 
33
    public AgentForwardedChannel(long socket) {
34
        super("auth-agent@openssh.com");
35
        this.socket = socket;
36
    }
37
 
38
    public void run() {
39
        try {
40
            byte[] buf = new byte[1024];
41
            while (true) {
42
                int result = Socket.recv(socket, buf, 0, buf.length);
43
                if (result == -Status.APR_EOF) {
44
                    break;
45
                } else if (result < Status.APR_SUCCESS) {
46
                    AgentServerProxy.throwException(result);
47
                }
48
                getInvertedIn().write(buf, 0, result);
49
                getInvertedIn().flush();
50
            }
51
        } catch (Exception e) {
52
            e.printStackTrace();
53
        } finally {
54
            close(false);
55
        }
56
    }
57
 
58
    @Override
59
    protected synchronized void doOpen() throws IOException {
60
        if (streaming == Streaming.Async) {
61
            throw new IllegalArgumentException("Asynchronous streaming isn't supported yet on this channel");
62
        }
63
        invertedIn = new ChannelOutputStream(this, remoteWindow, log, SshConstants.SSH_MSG_CHANNEL_DATA);
64
    }
65
 
66
    @Override
67
    protected synchronized void doCloseImmediately() {
68
        Socket.close(socket);
69
        super.doCloseImmediately();
70
    }
71
 
72
    protected synchronized void doWriteData(byte[] data, int off, int len) throws IOException {
73
        localWindow.consumeAndCheck(len);
74
        int result = Socket.send(socket, data, off, len);
75
        if (result < Status.APR_SUCCESS) {
76
            AgentServerProxy.throwException(result);
77
        }
78
 
79
    }
80
 
81
}