HandshakeInitializationPacket.js 3.2 KB

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