1/*
2 * Copyright (c) 2006-2011 Christian Plattner. All rights reserved.
3 * Please refer to the LICENSE.txt for licensing details.
4 */
5package ch.ethz.ssh2.packets;
6
7import java.io.IOException;
8
9/**
10 * PacketOpenSessionChannel.
11 *
12 * @author Christian Plattner
13 * @version 2.50, 03/15/10
14 */
15public class PacketOpenSessionChannel
16{
17	byte[] payload;
18
19	int channelID;
20	int initialWindowSize;
21	int maxPacketSize;
22
23	public PacketOpenSessionChannel(int channelID, int initialWindowSize,
24			int maxPacketSize)
25	{
26		this.channelID = channelID;
27		this.initialWindowSize = initialWindowSize;
28		this.maxPacketSize = maxPacketSize;
29	}
30
31	public PacketOpenSessionChannel(byte payload[], int off, int len) throws IOException
32	{
33		this.payload = new byte[len];
34		System.arraycopy(payload, off, this.payload, 0, len);
35
36		TypesReader tr = new TypesReader(payload);
37
38		int packet_type = tr.readByte();
39
40		if (packet_type != Packets.SSH_MSG_CHANNEL_OPEN)
41			throw new IOException("This is not a SSH_MSG_CHANNEL_OPEN! ("
42					+ packet_type + ")");
43
44		channelID = tr.readUINT32();
45		initialWindowSize = tr.readUINT32();
46		maxPacketSize = tr.readUINT32();
47
48		if (tr.remain() != 0)
49			throw new IOException("Padding in SSH_MSG_CHANNEL_OPEN packet!");
50	}
51
52	public byte[] getPayload()
53	{
54		if (payload == null)
55		{
56			TypesWriter tw = new TypesWriter();
57			tw.writeByte(Packets.SSH_MSG_CHANNEL_OPEN);
58			tw.writeString("session");
59			tw.writeUINT32(channelID);
60			tw.writeUINT32(initialWindowSize);
61			tw.writeUINT32(maxPacketSize);
62			payload = tw.getBytes();
63		}
64		return payload;
65	}
66}
67