tpkt.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. var inherits = require('util').inherits;
  20. var type = require('../core').type;
  21. var events = require('events');
  22. /**
  23. * Type of tpkt packet
  24. * Fastpath is use to shortcut RDP stack
  25. * @see http://msdn.microsoft.com/en-us/library/cc240621.aspx
  26. * @see http://msdn.microsoft.com/en-us/library/cc240589.aspx
  27. */
  28. var Action = {
  29. FASTPATH_ACTION_FASTPATH : 0x0,
  30. FASTPATH_ACTION_X224 : 0x3
  31. };
  32. /**
  33. * TPKT layer of rdp stack
  34. */
  35. function TPKT(transport) {
  36. this.transport = transport;
  37. // wait 2 bytes
  38. this.transport.expect(2);
  39. // next state is receive header
  40. var self = this;
  41. this.transport.once('data', function(s) {
  42. self.recvHeader(s);
  43. }).on('close', function() {
  44. self.emit('close');
  45. }).on('error', function (err) {
  46. self.emit('error', err);
  47. });
  48. }
  49. /**
  50. * inherit from a packet layer
  51. */
  52. inherits(TPKT, events.EventEmitter);
  53. /**
  54. * Receive correct packet as expected
  55. * @param s {type.Stream}
  56. */
  57. TPKT.prototype.recvHeader = function (s) {
  58. var version = new type.UInt8().read(s).value;
  59. var self = this;
  60. if(version === Action.FASTPATH_ACTION_X224) {
  61. new type.UInt8().read(s);
  62. this.transport.expect(2);
  63. this.transport.once('data', function(s) {
  64. self.recvExtendedHeader(s);
  65. });
  66. }
  67. else {
  68. this.secFlag = ((version >> 6) & 0x3);
  69. var length = new type.UInt8().read(s).value;
  70. if (length & 0x80) {
  71. this.transport.expect(1);
  72. this.transport.once('data', function(s) {
  73. self.recvExtendedFastPathHeader(s, length);
  74. });
  75. }
  76. else {
  77. this.transport.expect(length - 2);
  78. this.transport.once('data', function(s) {
  79. self.recvFastPath(s);
  80. });
  81. }
  82. }
  83. };
  84. /**
  85. * Receive second part of header packet
  86. * @param s {type.Stream}
  87. */
  88. TPKT.prototype.recvExtendedHeader = function (s) {
  89. var size = new type.UInt16Be().read(s);
  90. this.transport.expect(size.value - 4);
  91. //next state receive packet
  92. var self = this;
  93. this.transport.once('data', function(s) {
  94. self.recvData(s);
  95. });
  96. };
  97. /**
  98. * Receive data available for presentation layer
  99. * @param s {type.Stream}
  100. */
  101. TPKT.prototype.recvData = function (s) {
  102. this.emit('data', s);
  103. this.transport.expect(2);
  104. //next state receive header
  105. var self = this;
  106. this.transport.once('data', function(s) {
  107. self.recvHeader(s);
  108. });
  109. };
  110. /**
  111. * Read extended fastpath header
  112. * @param s {type.Stream}
  113. */
  114. TPKT.prototype.recvExtendedFastPathHeader = function (s, length) {
  115. var rightPart = new type.UInt8().read(s).value;
  116. var leftPart = length & ~0x80;
  117. var packetSize = (leftPart << 8) + rightPart;
  118. var self = this;
  119. this.transport.expect(packetSize - 3);
  120. this.transport.once('data', function(s) {
  121. self.recvFastPath(s);
  122. });
  123. };
  124. /**
  125. * Read fast path data
  126. * @param s {type.Stream}
  127. */
  128. TPKT.prototype.recvFastPath = function (s) {
  129. this.emit('fastPathData', this.secFlag, s);
  130. var self = this;
  131. this.transport.expect(2);
  132. this.transport.once('data', function(s) {
  133. self.recvHeader(s);
  134. });
  135. };
  136. /**
  137. * Send message throught TPKT layer
  138. * @param message {type.*}
  139. */
  140. TPKT.prototype.send = function(message) {
  141. this.transport.send(new type.Component([
  142. new type.UInt8(Action.FASTPATH_ACTION_X224),
  143. new type.UInt8(0),
  144. new type.UInt16Be(message.size() + 4),
  145. message
  146. ]));
  147. };
  148. /**
  149. * close stack
  150. */
  151. TPKT.prototype.close = function() {
  152. this.transport.close();
  153. };
  154. /**
  155. * Module exports
  156. */
  157. module.exports = TPKT;