EverydayTech Platform - Developer Reference
Complete Source Code Documentation - All Applications
Loading...
Searching...
No Matches
tpkt.js
Go to the documentation of this file.
1/*
2 * Copyright (c) 2014-2015 Sylvain Peyrefitte
3 *
4 * This file is part of node-rdpjs.
5 *
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.
10 *
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.
15 *
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/>.
18 */
19
20var inherits = require('util').inherits;
21var type = require('../core').type;
22var events = require('events');
23
24/**
25 * Type of tpkt packet
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
29 */
30var Action = {
31 FASTPATH_ACTION_FASTPATH : 0x0,
32 FASTPATH_ACTION_X224 : 0x3
33};
34
35/**
36 * TPKT layer of rdp stack
37 */
38function TPKT(transport) {
39 this.transport = transport;
40 // wait 2 bytes
41 this.transport.expect(2);
42 // next state is receive header
43 var self = this;
44 this.transport.once('data', function(s) {
45 self.recvHeader(s);
46 }).on('close', function() {
47 self.emit('close');
48 }).on('error', function (err) {
49 self.emit('error', err);
50 });
51}
52
53/**
54 * inherit from a packet layer
55 */
56inherits(TPKT, events.EventEmitter);
57
58/**
59 * Receive correct packet as expected
60 * @param s {type.Stream}
61 */
62TPKT.prototype.recvHeader = function (s) {
63 var version = new type.UInt8().read(s).value;
64 var self = this;
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);
70 });
71 }
72 else {
73 this.secFlag = ((version >> 6) & 0x3);
74 var length = new type.UInt8().read(s).value;
75 if (length & 0x80) {
76 this.transport.expect(1);
77 this.transport.once('data', function(s) {
78 self.recvExtendedFastPathHeader(s, length);
79 });
80 }
81 else {
82 this.transport.expect(length - 2);
83 this.transport.once('data', function(s) {
84 self.recvFastPath(s);
85 });
86 }
87 }
88};
89
90/**
91 * Receive second part of header packet
92 * @param s {type.Stream}
93 */
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
98 var self = this;
99 this.transport.once('data', function(s) {
100 self.recvData(s);
101 });
102};
103
104/**
105 * Receive data available for presentation layer
106 * @param s {type.Stream}
107 */
108TPKT.prototype.recvData = function (s) {
109 this.emit('data', s);
110 this.transport.expect(2);
111 //next state receive header
112 var self = this;
113 this.transport.once('data', function(s) {
114 self.recvHeader(s);
115 });
116};
117
118/**
119 * Read extended fastpath header
120 * @param s {type.Stream}
121 */
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;
126
127 var self = this;
128 this.transport.expect(packetSize - 3);
129 this.transport.once('data', function(s) {
130 self.recvFastPath(s);
131 });
132};
133
134/**
135 * Read fast path data
136 * @param s {type.Stream}
137 */
138TPKT.prototype.recvFastPath = function (s) {
139 this.emit('fastPathData', this.secFlag, s);
140 var self = this;
141 this.transport.expect(2);
142 this.transport.once('data', function(s) {
143 self.recvHeader(s);
144 });
145};
146
147/**
148 * Send message throught TPKT layer
149 * @param message {type.*}
150 */
151TPKT.prototype.send = function(message) {
152 this.transport.send(new type.Component([
153 new type.UInt8(Action.FASTPATH_ACTION_X224),
154 new type.UInt8(0),
155 new type.UInt16Be(message.size() + 4),
156 message
157 ]));
158};
159
160/**
161 * close stack
162 */
163TPKT.prototype.close = function() {
164 this.transport.close();
165};
166
167/**
168 * Module exports
169 */
170module.exports = TPKT;
171