HandshakeInitializationPacket.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. var Client = require('../constants/client');
  2. module.exports = HandshakeInitializationPacket;
  3. function HandshakeInitializationPacket(options) {
  4. options = options || {};
  5. this.protocolVersion = options.protocolVersion;
  6. this.serverVersion = options.serverVersion;
  7. this.threadId = options.threadId;
  8. this.scrambleBuff1 = options.scrambleBuff1;
  9. this.filler1 = options.filler1;
  10. this.serverCapabilities1 = options.serverCapabilities1;
  11. this.serverLanguage = options.serverLanguage;
  12. this.serverStatus = options.serverStatus;
  13. this.serverCapabilities2 = options.serverCapabilities2;
  14. this.scrambleLength = options.scrambleLength;
  15. this.filler2 = options.filler2;
  16. this.scrambleBuff2 = options.scrambleBuff2;
  17. this.filler3 = options.filler3;
  18. this.pluginData = options.pluginData;
  19. this.protocol41 = options.protocol41;
  20. if (this.protocol41) {
  21. // force set the bit in serverCapabilities1
  22. this.serverCapabilities1 |= Client.CLIENT_PROTOCOL_41;
  23. }
  24. }
  25. HandshakeInitializationPacket.prototype.parse = function(parser) {
  26. this.protocolVersion = parser.parseUnsignedNumber(1);
  27. this.serverVersion = parser.parseNullTerminatedString();
  28. this.threadId = parser.parseUnsignedNumber(4);
  29. this.scrambleBuff1 = parser.parseBuffer(8);
  30. this.filler1 = parser.parseFiller(1);
  31. this.serverCapabilities1 = parser.parseUnsignedNumber(2);
  32. this.serverLanguage = parser.parseUnsignedNumber(1);
  33. this.serverStatus = parser.parseUnsignedNumber(2);
  34. this.protocol41 = (this.serverCapabilities1 & (1 << 9)) > 0;
  35. if (this.protocol41) {
  36. this.serverCapabilities2 = parser.parseUnsignedNumber(2);
  37. this.scrambleLength = parser.parseUnsignedNumber(1);
  38. this.filler2 = parser.parseFiller(10);
  39. // scrambleBuff2 should be 0x00 terminated, but sphinx does not do this
  40. // so we assume scrambleBuff2 to be 12 byte and treat the next byte as a
  41. // filler byte.
  42. this.scrambleBuff2 = parser.parseBuffer(12);
  43. this.filler3 = parser.parseFiller(1);
  44. } else {
  45. this.filler2 = parser.parseFiller(13);
  46. }
  47. if (parser.reachedPacketEnd()) {
  48. return;
  49. }
  50. // According to the docs this should be 0x00 terminated, but MariaDB does
  51. // not do this, so we assume this string to be packet terminated.
  52. this.pluginData = parser.parsePacketTerminatedString();
  53. // However, if there is a trailing '\0', strip it
  54. var lastChar = this.pluginData.length - 1;
  55. if (this.pluginData[lastChar] === '\0') {
  56. this.pluginData = this.pluginData.substr(0, lastChar);
  57. }
  58. };
  59. HandshakeInitializationPacket.prototype.write = function(writer) {
  60. writer.writeUnsignedNumber(1, this.protocolVersion);
  61. writer.writeNullTerminatedString(this.serverVersion);
  62. writer.writeUnsignedNumber(4, this.threadId);
  63. writer.writeBuffer(this.scrambleBuff1);
  64. writer.writeFiller(1);
  65. writer.writeUnsignedNumber(2, this.serverCapabilities1);
  66. writer.writeUnsignedNumber(1, this.serverLanguage);
  67. writer.writeUnsignedNumber(2, this.serverStatus);
  68. if (this.protocol41) {
  69. writer.writeUnsignedNumber(2, this.serverCapabilities2);
  70. writer.writeUnsignedNumber(1, this.scrambleLength);
  71. writer.writeFiller(10);
  72. }
  73. writer.writeNullTerminatedBuffer(this.scrambleBuff2);
  74. if (this.pluginData !== undefined) {
  75. writer.writeNullTerminatedString(this.pluginData);
  76. }
  77. };
  78. HandshakeInitializationPacket.prototype.scrambleBuff = function() {
  79. var buffer = new Buffer(this.scrambleBuff1.length +
  80. (typeof this.scrambleBuff2 != "undefined" ? this.scrambleBuff2.length : 0));
  81. this.scrambleBuff1.copy(buffer);
  82. if (typeof this.scrambleBuff2 != "undefined") {
  83. this.scrambleBuff2.copy(buffer, this.scrambleBuff1.length);
  84. }
  85. return buffer;
  86. };