public SSHChannel OpenShell(ISSHChannelEventReceiver receiver) { _channel_entry = new TelnetChannel(this, socket, receiver); TelnetPacketBuilder pb = new TelnetPacketBuilder(receiver); PlainSocket ps = new PlainSocket(socket, pb); ps.RepeatAsyncRead(); return(_channel_entry); }
public override SSHChannel OpenShell(ISSHChannelEventReceiver receiver) { if (_shellID != -1) { throw new SSHException("A shell is opened already"); } _shellID = _channel_collection.RegisterChannelEventReceiver(null, receiver).LocalID; SendRequestPTY(); _executingShell = true; return(new SSH1Channel(this, ChannelType.Shell, _shellID)); }
public override void EstablishPortforwarding(ISSHChannelEventReceiver receiver, SSHChannel channel) { try { Channel ch = (Channel)receiver; ch.FixChannel(channel); ch.OnChannelReady(); ch.StartAsyncReceive(); } catch (Exception ex) { Debug.WriteLine(ex.StackTrace); Util.InterThreadWarning(ex.Message); } }
public Entry RegisterChannelEventReceiver(SSHChannel ch, ISSHChannelEventReceiver r) { lock (this) { Entry e = new Entry(ch, r, _channel_sequence++); if (_first == null) _first = e; else { e.Next = _first; _first = e; } _count++; return e; } }
public Entry RegisterChannelEventReceiver(SSHChannel ch, ISSHChannelEventReceiver r) { lock (this) { Entry e = new Entry(ch, r, _channel_sequence++); if (_first == null) { _first = e; } else { e.Next = _first; _first = e; } _count++; return(e); } }
protected ChannelEntry RegisterChannelEventReceiver(SSHChannel ch, ISSHChannelEventReceiver r) { lock (this) { ChannelEntry e = new ChannelEntry(); e._channel = ch; e._receiver = r; e._localID = _channel_sequence++; for (int i = 0; i < _channel_entries.Count; i++) { if (_channel_entries[i] == null) { _channel_entries[i] = e; return(e); } } _channel_entries.Add(e); return(e); } }
public override SSHChannel ForwardPort(ISSHChannelEventReceiver receiver, string remote_host, int remote_port, string originator_host, int originator_port) { if (_shellID == -1) { ExecShell(); _shellID = RegisterChannelEventReceiver(null, new SSH1DummyReceiver())._localID; } int local_id = this.RegisterChannelEventReceiver(null, receiver)._localID; SSH1DataWriter writer = new SSH1DataWriter(); writer.Write(local_id); //channel id is fixed to 0 writer.Write(remote_host); writer.Write(remote_port); //originator is specified only if SSH_PROTOFLAG_HOST_IN_FWD_OPEN is specified //writer.Write(originator_host); SSH1Packet SSH1Packet = SSH1Packet.FromPlainPayload(PacketType.SSH_MSG_PORT_OPEN, writer.ToByteArray()); SSH1Packet.WriteTo(_stream, _tCipher); return(new SSH1Channel(this, ChannelType.ForwardedLocalToRemote, local_id)); }
internal void ProcessPacket(ISSHChannelEventReceiver receiver, PacketType pt, int data_length, SSH2DataReader re) { //NOTE: the offset of 're' is next to 'receipiant channel' field _leftWindowSize -= data_length; while(_leftWindowSize <= _windowSize) { SSH2DataWriter adj = new SSH2DataWriter(); adj.WritePacketType(PacketType.SSH_MSG_CHANNEL_WINDOW_ADJUST); adj.Write(_remoteID); adj.Write(_windowSize); TransmitPacket(adj.ToByteArray()); _leftWindowSize += _windowSize; //Debug.WriteLine("Window size is adjusted to " + _leftWindowSize); } if(pt==PacketType.SSH_MSG_CHANNEL_WINDOW_ADJUST) { int w = re.ReadInt32(); //Debug.WriteLine(String.Format("Window Adjust +={0}",w)); } else if(_negotiationStatus!=0) { //when the negotiation is not completed if(_type==ChannelType.Shell) OpenShell(receiver, pt, re); else if(_type==ChannelType.ForwardedLocalToRemote) ReceivePortForwardingResponse(receiver, pt, re); else if(_type==ChannelType.Session) EstablishSession(receiver, pt, re); } else { switch(pt) { case PacketType.SSH_MSG_CHANNEL_DATA: { int len = re.ReadInt32(); receiver.OnData(re.Image, re.Offset, len); } break; case PacketType.SSH_MSG_CHANNEL_EXTENDED_DATA: { int t = re.ReadInt32(); byte[] data = re.ReadString(); receiver.OnExtendedData(t, data); } break; case PacketType.SSH_MSG_CHANNEL_REQUEST: { string request = Encoding.ASCII.GetString(re.ReadString()); bool reply = re.ReadBool(); if(request=="exit-status") { int status = re.ReadInt32(); } else if(reply) { //we reject unknown requests including keep-alive check SSH2DataWriter wr = new SSH2DataWriter(); wr.Write((byte)PacketType.SSH_MSG_CHANNEL_FAILURE); wr.Write(_remoteID); TransmitPacket(wr.ToByteArray()); } } break; case PacketType.SSH_MSG_CHANNEL_EOF: receiver.OnChannelEOF(); break; case PacketType.SSH_MSG_CHANNEL_CLOSE: _connection.UnregisterChannelEventReceiver(_localID); receiver.OnChannelClosed(); break; case PacketType.SSH_MSG_CHANNEL_FAILURE: case PacketType.SSH_MSG_CHANNEL_SUCCESS: receiver.OnMiscPacket((byte)pt, re.Image, re.Offset, re.Rest); break; default: receiver.OnMiscPacket((byte)pt, re.Image, re.Offset, re.Rest); Debug.WriteLine("Unknown Packet "+pt); break; } } }
public Entry(SSHChannel ch, ISSHChannelEventReceiver r, int seq) { _channel = ch; _receiver = r; _localID = seq; }
protected ChannelEntry RegisterChannelEventReceiver(SSHChannel ch, ISSHChannelEventReceiver r) { lock(this) { ChannelEntry e = new ChannelEntry(); e._channel = ch; e._receiver = r; e._localID = _channel_sequence++; for(int i=0; i<_channel_entries.Count; i++) { if(_channel_entries[i]==null) { _channel_entries[i] = e; return e; } } _channel_entries.Add(e); return e; } }
/** * forwards the remote end to another host */ public abstract SSHChannel ForwardPort(ISSHChannelEventReceiver receiver, string remote_host, int remote_port, string originator_host, int originator_port);
public virtual void EstablishPortforwarding(ISSHChannelEventReceiver receiver, SSHChannel channel) { }
public override SSHChannel OpenShell(ISSHChannelEventReceiver receiver) { if (_shellID != -1) throw new SSHException("A shell is opened already"); _shellID = _channel_collection.RegisterChannelEventReceiver(null, receiver).LocalID; SendRequestPTY(); _executingShell = true; return new SSH1Channel(this, ChannelType.Shell, _shellID); }
public override SSHChannel OpenShell(ISSHChannelEventReceiver receiver) { return DoExecCommandInternal(receiver, ChannelType.Shell, null, "opening shell"); }
public abstract SSHChannel DoExecCommand(ISSHChannelEventReceiver receiver, string command);
internal void AsyncReceivePacket(SSH1Packet p) { try { int len = 0, channel = 0; switch (p.Type) { case PacketType.SSH_SMSG_STDOUT_DATA: len = SSHUtil.ReadInt32(p.Data, 0); FindChannelEntry(_shellID)._receiver.OnData(p.Data, 4, len); break; case PacketType.SSH_SMSG_STDERR_DATA: { SSH1DataReader re = new SSH1DataReader(p.Data); FindChannelEntry(_shellID)._receiver.OnExtendedData((int)PacketType.SSH_SMSG_STDERR_DATA, re.ReadString()); } break; case PacketType.SSH_MSG_CHANNEL_DATA: channel = SSHUtil.ReadInt32(p.Data, 0); len = SSHUtil.ReadInt32(p.Data, 4); FindChannelEntry(channel)._receiver.OnData(p.Data, 8, len); break; case PacketType.SSH_MSG_PORT_OPEN: this.ProcessPortforwardingRequest(_eventReceiver, p); break; case PacketType.SSH_MSG_CHANNEL_CLOSE: { channel = SSHUtil.ReadInt32(p.Data, 0); ISSHChannelEventReceiver r = FindChannelEntry(channel)._receiver; UnregisterChannelEventReceiver(channel); r.OnChannelClosed(); } break; case PacketType.SSH_MSG_CHANNEL_CLOSE_CONFIRMATION: channel = SSHUtil.ReadInt32(p.Data, 0); break; case PacketType.SSH_MSG_DISCONNECT: _eventReceiver.OnConnectionClosed(); break; case PacketType.SSH_SMSG_EXITSTATUS: FindChannelEntry(_shellID)._receiver.OnChannelClosed(); break; case PacketType.SSH_MSG_DEBUG: { SSH1DataReader re = new SSH1DataReader(p.Data); _eventReceiver.OnDebugMessage(false, re.ReadString()); } break; case PacketType.SSH_MSG_IGNORE: { SSH1DataReader re = new SSH1DataReader(p.Data); _eventReceiver.OnIgnoreMessage(re.ReadString()); } break; case PacketType.SSH_MSG_CHANNEL_OPEN_CONFIRMATION: { int local = SSHUtil.ReadInt32(p.Data, 0); int remote = SSHUtil.ReadInt32(p.Data, 4); FindChannelEntry(local)._receiver.OnChannelReady(); } break; case PacketType.SSH_SMSG_SUCCESS: if (_executingShell) { ExecShell(); this.FindChannelEntry(_shellID)._receiver.OnChannelReady(); _executingShell = false; } break; default: _eventReceiver.OnUnknownMessage((byte)p.Type, p.Data); break; } } catch (Exception ex) { if (!_closed) { _eventReceiver.OnError(ex, ex.Message); } } }
public void EstablishPortforwarding(ISSHChannelEventReceiver receiver, SSHChannel channel) { Channel = channel; }
/// <summary> /// Sending exec command for SCP. /// </summary> /// <param name="receiver"></param> /// <param name="command"></param> /// <returns></returns> public override SSHChannel DoExecCommand(ISSHChannelEventReceiver receiver, string command) { SendExecCommand(); return(null); }
public TelnetChannel(TelnetConnection parent, StreamSocket socket, ISSHChannelEventReceiver r) { _parent = parent; _socket = socket; _receiver = r; }
private void ReceivePortForwardingResponse(ISSHChannelEventReceiver receiver, PacketType pt, SSH2DataReader reader) { if(_negotiationStatus==1) { if(pt!=PacketType.SSH_MSG_CHANNEL_OPEN_CONFIRMATION) { if(pt!=PacketType.SSH_MSG_CHANNEL_OPEN_FAILURE) receiver.OnChannelError(null, "opening channel failed; packet type="+pt); else { int errcode = reader.ReadInt32(); string msg = Encoding.ASCII.GetString(reader.ReadString()); receiver.OnChannelError(null, msg); } Close(); } else { _remoteID = reader.ReadInt32(); _serverMaxPacketSize = reader.ReadInt32(); _negotiationStatus = 0; receiver.OnChannelReady(); } } else Debug.Assert(false); }
// open new channel for SCP public override SSHChannel DoExecCommand(ISSHChannelEventReceiver receiver, string command) { return DoExecCommandInternal(receiver, ChannelType.ExecCommand, command, "executing " + command); }
public override SSHChannel OpenShell(ISSHChannelEventReceiver receiver) { //open channel SSH2DataWriter wr = new SSH2DataWriter(); wr.WritePacketType(PacketType.SSH_MSG_CHANNEL_OPEN); wr.Write("session"); int local_channel = this.RegisterChannelEventReceiver(null, receiver)._localID; wr.Write(local_channel); wr.Write(_param.WindowSize); //initial window size int windowsize = _param.WindowSize; wr.Write(_param.MaxPacketSize); //max packet size SSH2Channel channel = new SSH2Channel(this, ChannelType.Shell, local_channel); TransmitPacket(wr.ToByteArray()); return channel; }
// open subsystem such as NETCONF public SSHChannel OpenSubsystem(ISSHChannelEventReceiver receiver, string subsystem) { return DoExecCommandInternal(receiver, ChannelType.Subsystem, subsystem, "subsystem " + subsystem); }
// sending exec command for SCP // TODO: まだ実装中です public override SSHChannel DoExecCommand(ISSHChannelEventReceiver receiver, string command) { //_executingExecCmd = true; SendExecCommand(); return null; }
//open channel private SSHChannel DoExecCommandInternal(ISSHChannelEventReceiver receiver, ChannelType channel_type, string command, string message) { SSH2DataWriter wr = OpenTransmissionPacket(); wr.WritePacketType(PacketType.SSH_MSG_CHANNEL_OPEN); wr.Write("session"); int local_channel = this.ChannelCollection.RegisterChannelEventReceiver(null, receiver).LocalID; wr.Write(local_channel); wr.Write(_param.WindowSize); //initial window size int windowsize = _param.WindowSize; wr.Write(_param.MaxPacketSize); //max packet size SSH2Channel channel = new SSH2Channel(this, channel_type, local_channel, command); TraceTransmissionEvent(PacketType.SSH_MSG_CHANNEL_OPEN, "executing command"); TransmitPacket(wr); return channel; }
public override SSHChannel ForwardPort(ISSHChannelEventReceiver receiver, string remote_host, int remote_port, string originator_host, int originator_port) { SSH2DataWriter wr = OpenTransmissionPacket(); wr.WritePacketType(PacketType.SSH_MSG_CHANNEL_OPEN); wr.Write("direct-tcpip"); int local_id = this.ChannelCollection.RegisterChannelEventReceiver(null, receiver).LocalID; wr.Write(local_id); wr.Write(_param.WindowSize); //initial window size int windowsize = _param.WindowSize; wr.Write(_param.MaxPacketSize); //max packet size wr.Write(remote_host); wr.Write(remote_port); wr.Write(originator_host); wr.Write(originator_port); SSH2Channel channel = new SSH2Channel(this, ChannelType.ForwardedLocalToRemote, local_id, null); TraceTransmissionEvent(PacketType.SSH_MSG_CHANNEL_OPEN, "opening a forwarded port : host={0} port={1}", remote_host, remote_port); TransmitPacket(wr); return channel; }
internal void ProcessPacket(ISSHChannelEventReceiver receiver, PacketType pt, int data_length, SSH2DataReader re) { //NOTE: the offset of 're' is next to 'receipiant channel' field AdjustWindowSize(pt, data_length); //SSH_MSG_CHANNEL_WINDOW_ADJUST comes before the complete of channel establishment if(pt==PacketType.SSH_MSG_CHANNEL_WINDOW_ADJUST) { int w = re.ReadInt32(); //some servers may not send SSH_MSG_CHANNEL_WINDOW_ADJUST. //it is dangerous to wait this message in send procedure _allowedDataSize += w; if(_connection.IsEventTracerAvailable) _connection.TraceReceptionEvent("SSH_MSG_CHANNEL_WINDOW_ADJUST", "adjusted to {0} by increasing {1}", _allowedDataSize, w); return; } if(_negotiationStatus!=NegotiationStatus.Ready) //when the negotiation is not completed ProgressChannelNegotiation(receiver, pt, re); else ProcessChannelLocalData(receiver, pt, re); }
/** * opens a pseudo terminal */ public abstract SSHChannel OpenShell(ISSHChannelEventReceiver receiver);
//Progress the state of this channel establishment negotiation private void ProgressChannelNegotiation(ISSHChannelEventReceiver receiver, PacketType pt, SSH2DataReader re) { if(_type==ChannelType.Shell) OpenShellOrSubsystem(receiver, pt, re, "shell"); else if(_type==ChannelType.ForwardedLocalToRemote) ReceivePortForwardingResponse(receiver, pt, re); else if(_type==ChannelType.Session) EstablishSession(receiver, pt, re); else if(_type==ChannelType.ExecCommand) // for SCP ExecCommand(receiver, pt, re); else if(_type==ChannelType.Subsystem) OpenShellOrSubsystem(receiver, pt, re, "subsystem"); }
public void EstablishPortforwarding(ISSHChannelEventReceiver rec, SSHChannel channel) { this._pf = channel; }
private void ProcessChannelLocalData(ISSHChannelEventReceiver receiver, PacketType pt, SSH2DataReader re) { switch(pt) { case PacketType.SSH_MSG_CHANNEL_DATA: { int len = re.ReadInt32(); receiver.OnData(re.Image, re.Offset, len); } break; case PacketType.SSH_MSG_CHANNEL_EXTENDED_DATA: { int t = re.ReadInt32(); byte[] data = re.ReadString(); receiver.OnExtendedData(t, data); } break; case PacketType.SSH_MSG_CHANNEL_REQUEST: { string request = Encoding.ASCII.GetString(re.ReadString()); bool reply = re.ReadBool(); if(request=="exit-status") { int status = re.ReadInt32(); } else if(reply) { //we reject unknown requests including keep-alive check SSH2DataWriter wr = new SSH2DataWriter(); wr.WritePacketType(PacketType.SSH_MSG_CHANNEL_FAILURE); wr.Write(_remoteID); TransmitPayload(wr.ToByteArray()); } } break; case PacketType.SSH_MSG_CHANNEL_EOF: receiver.OnChannelEOF(); break; case PacketType.SSH_MSG_CHANNEL_CLOSE: _connection.ChannelCollection.UnregisterChannelEventReceiver(_localID); receiver.OnChannelClosed(); break; case PacketType.SSH_MSG_CHANNEL_FAILURE: receiver.OnMiscPacket((byte)pt, re.Image, re.Offset, re.Rest); break; default: receiver.OnMiscPacket((byte)pt, re.Image, re.Offset, re.Rest); Debug.WriteLine("Unknown Packet "+pt); break; } }
public SSHChannel ForwardPort(ISSHChannelEventReceiver receiver, string remote_host, int remote_port, string originator_host, int originator_port) { throw new NotImplementedException(); }
private void OpenShellOrSubsystem(ISSHChannelEventReceiver receiver, PacketType pt, SSH2DataReader reader, string scheme) { if(_negotiationStatus==NegotiationStatus.WaitingChannelConfirmation) { if(pt!=PacketType.SSH_MSG_CHANNEL_OPEN_CONFIRMATION) { if(pt!=PacketType.SSH_MSG_CHANNEL_OPEN_FAILURE) receiver.OnChannelError(new SSHException("opening channel failed; packet type="+pt)); else { int errcode = reader.ReadInt32(); string msg = Encoding.ASCII.GetString(reader.ReadString()); receiver.OnChannelError(new SSHException(msg)); } Close(); } else { _remoteID = reader.ReadInt32(); _allowedDataSize = reader.ReadInt32(); _serverMaxPacketSize = reader.ReadInt32(); //open pty SSH2DataWriter wr = new SSH2DataWriter(); SSHConnectionParameter param = _connection.Param; wr.WritePacketType(PacketType.SSH_MSG_CHANNEL_REQUEST); wr.Write(_remoteID); wr.Write("pty-req"); wr.Write(true); wr.Write(param.TerminalName); wr.Write(param.TerminalWidth); wr.Write(param.TerminalHeight); wr.Write(param.TerminalPixelWidth); wr.Write(param.TerminalPixelHeight); wr.WriteAsString(new byte[0]); if(_connection.IsEventTracerAvailable) _connection.TraceTransmissionEvent(PacketType.SSH_MSG_CHANNEL_REQUEST, "pty-req", "terminal={0} width={1} height={2}", param.TerminalName, param.TerminalWidth, param.TerminalHeight); TransmitPayload(wr.ToByteArray()); _negotiationStatus = NegotiationStatus.WaitingPtyReqConfirmation; } } else if(_negotiationStatus==NegotiationStatus.WaitingPtyReqConfirmation) { if(pt!=PacketType.SSH_MSG_CHANNEL_SUCCESS) { receiver.OnChannelError(new SSHException("opening pty failed")); Close(); } else { //agent request (optional) if(_connection.Param.AgentForward!=null) { SSH2DataWriter wr = new SSH2DataWriter(); wr.WritePacketType(PacketType.SSH_MSG_CHANNEL_REQUEST); wr.Write(_remoteID); wr.Write("*****@*****.**"); wr.Write(true); _connection.TraceTransmissionEvent(PacketType.SSH_MSG_CHANNEL_REQUEST, "auth-agent-req", ""); TransmitPayload(wr.ToByteArray()); _negotiationStatus = NegotiationStatus.WaitingAuthAgentReqConfirmation; } else { OpenScheme(scheme); _negotiationStatus = NegotiationStatus.WaitingShellConfirmation; } } } else if(_negotiationStatus==NegotiationStatus.WaitingAuthAgentReqConfirmation) { if(pt!=PacketType.SSH_MSG_CHANNEL_SUCCESS && pt!=PacketType.SSH_MSG_CHANNEL_FAILURE) { receiver.OnChannelError(new SSHException("auth-agent-req error")); Close(); } else { //auth-agent-req is optional _connection.SetAgentForwardConfirmed(pt==PacketType.SSH_MSG_CHANNEL_SUCCESS); _connection.TraceReceptionEvent(pt, "auth-agent-req"); OpenScheme(scheme); _negotiationStatus = NegotiationStatus.WaitingShellConfirmation; } } else if(_negotiationStatus==NegotiationStatus.WaitingShellConfirmation) { if(pt!=PacketType.SSH_MSG_CHANNEL_SUCCESS) { receiver.OnChannelError(new SSHException("Opening shell failed: packet type="+pt.ToString())); Close(); } else { receiver.OnChannelReady(); _negotiationStatus = NegotiationStatus.Ready; //goal! } } }
private void OpenShell(ISSHChannelEventReceiver receiver, PacketType pt, SSH2DataReader reader) { if(_negotiationStatus==3) { if(pt!=PacketType.SSH_MSG_CHANNEL_OPEN_CONFIRMATION) { if(pt!=PacketType.SSH_MSG_CHANNEL_OPEN_FAILURE) receiver.OnChannelError(null, "opening channel failed; packet type="+pt); else { int errcode = reader.ReadInt32(); string msg = Encoding.ASCII.GetString(reader.ReadString()); receiver.OnChannelError(null, msg); } Close(); } else { _remoteID = reader.ReadInt32(); _serverMaxPacketSize = reader.ReadInt32(); //open pty SSH2DataWriter wr = new SSH2DataWriter(); wr.WritePacketType(PacketType.SSH_MSG_CHANNEL_REQUEST); wr.Write(_remoteID); wr.Write("pty-req"); wr.Write(true); wr.Write(_connection.Param.TerminalName); wr.Write(_connection.Param.TerminalWidth); wr.Write(_connection.Param.TerminalHeight); wr.Write(_connection.Param.TerminalPixelWidth); wr.Write(_connection.Param.TerminalPixelHeight); wr.WriteAsString(new byte[0]); TransmitPacket(wr.ToByteArray()); _negotiationStatus = 2; } } else if(_negotiationStatus==2) { if(pt!=PacketType.SSH_MSG_CHANNEL_SUCCESS) { receiver.OnChannelError(null, "opening pty failed"); Close(); } else { //open shell SSH2DataWriter wr = new SSH2DataWriter(); wr.Write((byte)PacketType.SSH_MSG_CHANNEL_REQUEST); wr.Write(_remoteID); wr.Write("shell"); wr.Write(true); TransmitPacket(wr.ToByteArray()); _negotiationStatus = 1; } } else if(_negotiationStatus==1) { if(pt!=PacketType.SSH_MSG_CHANNEL_SUCCESS) { receiver.OnChannelError(null, "Opening shell failed: packet type="+pt.ToString()); Close(); } else { receiver.OnChannelReady(); _negotiationStatus = 0; //goal! } } else Debug.Assert(false); }
// sending "exec" service for SCP protocol. private void ExecCommand(ISSHChannelEventReceiver receiver, PacketType pt, SSH2DataReader reader) { if(_negotiationStatus==NegotiationStatus.WaitingChannelConfirmation) { if(pt!=PacketType.SSH_MSG_CHANNEL_OPEN_CONFIRMATION) { if(pt!=PacketType.SSH_MSG_CHANNEL_OPEN_FAILURE) receiver.OnChannelError(new SSHException("opening channel failed; packet type="+pt)); else { int errcode = reader.ReadInt32(); string msg = Encoding.ASCII.GetString(reader.ReadString()); receiver.OnChannelError(new SSHException(msg)); } Close(); } else { _remoteID = reader.ReadInt32(); _allowedDataSize = reader.ReadInt32(); _serverMaxPacketSize = reader.ReadInt32(); // exec command SSH2DataWriter wr = new SSH2DataWriter(); SSHConnectionParameter param = _connection.Param; wr.WritePacketType(PacketType.SSH_MSG_CHANNEL_REQUEST); wr.Write(_remoteID); wr.Write("exec"); // "exec" wr.Write(false); // want confirm is disabled. (*) wr.Write(_command); if (_connection.IsEventTracerAvailable) _connection.TraceTransmissionEvent("exec command","cmd={0}", _command); TransmitPayload(wr.ToByteArray()); //confirmation is omitted receiver.OnChannelReady(); _negotiationStatus = NegotiationStatus.Ready; //goal! } } else if(_negotiationStatus==NegotiationStatus.WaitingExecCmdConfirmation) { if(pt!=PacketType.SSH_MSG_CHANNEL_DATA) { receiver.OnChannelError(new SSHException("exec command failed")); Close(); } else { receiver.OnChannelReady(); _negotiationStatus = NegotiationStatus.Ready; //goal! } } else throw new SSHException("internal state error"); }
public override SSHChannel ForwardPort(ISSHChannelEventReceiver receiver, string remote_host, int remote_port, string originator_host, int originator_port) { SSH2DataWriter wr = new SSH2DataWriter(); wr.WritePacketType(PacketType.SSH_MSG_CHANNEL_OPEN); wr.Write("direct-tcpip"); int local_id = RegisterChannelEventReceiver(null, receiver)._localID; wr.Write(local_id); wr.Write(_param.WindowSize); //initial window size int windowsize = _param.WindowSize; wr.Write(_param.MaxPacketSize); //max packet size wr.Write(remote_host); wr.Write(remote_port); wr.Write(originator_host); wr.Write(originator_port); SSH2Channel channel = new SSH2Channel(this, ChannelType.ForwardedLocalToRemote, local_id); TransmitPacket(wr.ToByteArray()); return channel; }
private void EstablishSession(ISSHChannelEventReceiver receiver, PacketType pt, SSH2DataReader reader) { if(_negotiationStatus==NegotiationStatus.WaitingChannelConfirmation) { if(pt!=PacketType.SSH_MSG_CHANNEL_OPEN_CONFIRMATION) { if(pt!=PacketType.SSH_MSG_CHANNEL_OPEN_FAILURE) receiver.OnChannelError(new SSHException("opening channel failed; packet type="+pt)); else { int remote_id = reader.ReadInt32(); int errcode = reader.ReadInt32(); string msg = Encoding.ASCII.GetString(reader.ReadString()); receiver.OnChannelError(new SSHException(msg)); } Close(); } else { _remoteID = reader.ReadInt32(); _serverMaxPacketSize = reader.ReadInt32(); _negotiationStatus = NegotiationStatus.Ready; receiver.OnChannelReady(); } } else throw new SSHException("internal state error"); }
// sending exec command for SCP // TODO: まだ実装中です public override SSHChannel DoExecCommand(ISSHChannelEventReceiver receiver, string command) { //_executingExecCmd = true; SendExecCommand(); return(null); }
public override SSHChannel DoExecCommand(ISSHChannelEventReceiver receiver, string command) { throw new Exception("The method or operation is not implemented."); }
internal void AsyncReceivePacket(DataFragment data) { try { int len = 0, channel = 0; SSH1DataReader re = new SSH1DataReader(data); PacketType pt = re.ReadPacketType(); switch (pt) { case PacketType.SSH_SMSG_STDOUT_DATA: len = re.ReadInt32(); _channel_collection.FindChannelEntry(_shellID).Receiver.OnData(re.Image, re.Offset, len); break; case PacketType.SSH_SMSG_STDERR_DATA: { _channel_collection.FindChannelEntry(_shellID).Receiver.OnExtendedData((int)PacketType.SSH_SMSG_STDERR_DATA, re.ReadString()); } break; case PacketType.SSH_MSG_CHANNEL_DATA: channel = re.ReadInt32(); len = re.ReadInt32(); _channel_collection.FindChannelEntry(channel).Receiver.OnData(re.Image, re.Offset, len); break; case PacketType.SSH_MSG_PORT_OPEN: ProcessPortforwardingRequest(_eventReceiver, re); break; case PacketType.SSH_MSG_CHANNEL_CLOSE: { channel = re.ReadInt32(); ISSHChannelEventReceiver r = _channel_collection.FindChannelEntry(channel).Receiver; _channel_collection.UnregisterChannelEventReceiver(channel); r.OnChannelClosed(); } break; case PacketType.SSH_MSG_CHANNEL_CLOSE_CONFIRMATION: channel = re.ReadInt32(); break; case PacketType.SSH_MSG_DISCONNECT: _eventReceiver.OnConnectionClosed(); break; case PacketType.SSH_SMSG_EXITSTATUS: _channel_collection.FindChannelEntry(_shellID).Receiver.OnChannelClosed(); break; case PacketType.SSH_MSG_DEBUG: _eventReceiver.OnDebugMessage(false, re.ReadString()); break; case PacketType.SSH_MSG_IGNORE: _eventReceiver.OnIgnoreMessage(re.ReadString()); break; case PacketType.SSH_MSG_CHANNEL_OPEN_CONFIRMATION: { int local = re.ReadInt32(); int remote = re.ReadInt32(); _channel_collection.FindChannelEntry(local).Receiver.OnChannelReady(); } break; case PacketType.SSH_SMSG_SUCCESS: if (_executingShell) { ExecShell(); _channel_collection.FindChannelEntry(_shellID).Receiver.OnChannelReady(); _executingShell = false; } break; default: _eventReceiver.OnUnknownMessage((byte)pt, re.ReadAll()); break; } } catch (Exception ex) { _eventReceiver.OnError(ex); } }
public override SSHChannel ForwardPort(ISSHChannelEventReceiver receiver, string remote_host, int remote_port, string originator_host, int originator_port) { if (_shellID == -1) { ExecShell(); _shellID = _channel_collection.RegisterChannelEventReceiver(null, new SSH1DummyReceiver()).LocalID; } int local_id = _channel_collection.RegisterChannelEventReceiver(null, receiver).LocalID; Transmit( new SSH1Packet(SSH1PacketType.SSH_MSG_PORT_OPEN) .WriteInt32(local_id) //channel id is fixed to 0 .WriteString(remote_host) .WriteInt32(remote_port) //originator is specified only if SSH_PROTOFLAG_HOST_IN_FWD_OPEN is specified //writer.Write(originator_host); ); TraceTransmissionEvent(SSH1PacketType.SSH_MSG_PORT_OPEN, "open forwarded port: host={0} port={1}", remote_host, remote_port); return new SSH1Channel(this, ChannelType.ForwardedLocalToRemote, local_id); }
public override SSHChannel ForwardPort(ISSHChannelEventReceiver receiver, string remote_host, int remote_port, string originator_host, int originator_port) { throw new Exception("The method or operation is not implemented."); }
public SSHChannel StartShell(ISSHChannelEventReceiver receiver) { return(m_conn.OpenShell(receiver)); }
public TelnetPacketBuilder(ISSHChannelEventReceiver handler) { this._handler = handler; }
public override SSHChannel OpenShell(ISSHChannelEventReceiver receiver) { throw new Exception("The method or operation is not implemented."); }
public void EstablishPortforwarding(ISSHChannelEventReceiver rec, SSHChannel channel) { _pf = channel; }