2 * Copyright (c) 2014-2015 Sylvain Peyrefitte
4 * This file is part of node-rdpjs.
6 * node-rdpjs is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20var inherits = require('util').inherits;
21var type = require('../core').type;
22var events = require('events');
26 * Fastpath is use to shortcut RDP stack
27 * @see http://msdn.microsoft.com/en-us/library/cc240621.aspx
28 * @see http://msdn.microsoft.com/en-us/library/cc240589.aspx
31 FASTPATH_ACTION_FASTPATH : 0x0,
32 FASTPATH_ACTION_X224 : 0x3
36 * TPKT layer of rdp stack
38function TPKT(transport) {
39 this.transport = transport;
41 this.transport.expect(2);
42 // next state is receive header
44 this.transport.once('data', function(s) {
46 }).on('close', function() {
48 }).on('error', function (err) {
49 self.emit('error', err);
54 * inherit from a packet layer
56inherits(TPKT, events.EventEmitter);
59 * Receive correct packet as expected
60 * @param s {type.Stream}
62TPKT.prototype.recvHeader = function (s) {
63 var version = new type.UInt8().read(s).value;
65 if(version === Action.FASTPATH_ACTION_X224) {
66 new type.UInt8().read(s);
67 this.transport.expect(2);
68 this.transport.once('data', function(s) {
69 self.recvExtendedHeader(s);
73 this.secFlag = ((version >> 6) & 0x3);
74 var length = new type.UInt8().read(s).value;
76 this.transport.expect(1);
77 this.transport.once('data', function(s) {
78 self.recvExtendedFastPathHeader(s, length);
82 this.transport.expect(length - 2);
83 this.transport.once('data', function(s) {
91 * Receive second part of header packet
92 * @param s {type.Stream}
94TPKT.prototype.recvExtendedHeader = function (s) {
95 var size = new type.UInt16Be().read(s);
96 this.transport.expect(size.value - 4);
97 //next state receive packet
99 this.transport.once('data', function(s) {
105 * Receive data available for presentation layer
106 * @param s {type.Stream}
108TPKT.prototype.recvData = function (s) {
109 this.emit('data', s);
110 this.transport.expect(2);
111 //next state receive header
113 this.transport.once('data', function(s) {
119 * Read extended fastpath header
120 * @param s {type.Stream}
122TPKT.prototype.recvExtendedFastPathHeader = function (s, length) {
123 var rightPart = new type.UInt8().read(s).value;
124 var leftPart = length & ~0x80;
125 var packetSize = (leftPart << 8) + rightPart;
128 this.transport.expect(packetSize - 3);
129 this.transport.once('data', function(s) {
130 self.recvFastPath(s);
135 * Read fast path data
136 * @param s {type.Stream}
138TPKT.prototype.recvFastPath = function (s) {
139 this.emit('fastPathData', this.secFlag, s);
141 this.transport.expect(2);
142 this.transport.once('data', function(s) {
148 * Send message throught TPKT layer
149 * @param message {type.*}
151TPKT.prototype.send = function(message) {
152 this.transport.send(new type.Component([
153 new type.UInt8(Action.FASTPATH_ACTION_X224),
155 new type.UInt16Be(message.size() + 4),
163TPKT.prototype.close = function() {
164 this.transport.close();
170module.exports = TPKT;