Ohm-Management - Projektarbeit B-ME
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

message.js 2.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. // Copyright 2011 Mark Cavage, Inc. All rights reserved.
  2. var assert = require('assert-plus');
  3. var util = require('util');
  4. var asn1 = require('asn1');
  5. var Control = require('../controls').Control;
  6. var Protocol = require('../protocol');
  7. ///--- Globals
  8. var Ber = asn1.Ber;
  9. var BerReader = asn1.BerReader;
  10. var BerWriter = asn1.BerWriter;
  11. var getControl = require('../controls').getControl;
  12. ///--- API
  13. /**
  14. * LDAPMessage structure.
  15. *
  16. * @param {Object} options stuff.
  17. */
  18. function LDAPMessage(options) {
  19. assert.object(options);
  20. this.messageID = options.messageID || 0;
  21. this.protocolOp = options.protocolOp || undefined;
  22. this.controls = options.controls ? options.controls.slice(0) : [];
  23. this.log = options.log;
  24. }
  25. Object.defineProperties(LDAPMessage.prototype, {
  26. id: {
  27. get: function getId() { return this.messageID; },
  28. configurable: false
  29. },
  30. dn: {
  31. get: function getDN() { return this._dn || ''; },
  32. configurable: false
  33. },
  34. type: {
  35. get: function getType() { return 'LDAPMessage'; },
  36. configurable: false
  37. },
  38. json: {
  39. get: function () {
  40. var out = this._json({
  41. messageID: this.messageID,
  42. protocolOp: this.type
  43. });
  44. out.controls = this.controls;
  45. return out;
  46. },
  47. configurable: false
  48. }
  49. });
  50. LDAPMessage.prototype.toString = function () {
  51. return JSON.stringify(this.json);
  52. };
  53. LDAPMessage.prototype.parse = function (ber) {
  54. assert.ok(ber);
  55. if (this.log.trace())
  56. this.log.trace('parse: data=%s', util.inspect(ber.buffer));
  57. // Delegate off to the specific type to parse
  58. this._parse(ber, ber.length);
  59. // Look for controls
  60. if (ber.peek() === 0xa0) {
  61. ber.readSequence();
  62. var end = ber.offset + ber.length;
  63. while (ber.offset < end) {
  64. var c = getControl(ber);
  65. if (c)
  66. this.controls.push(c);
  67. }
  68. }
  69. if (this.log.trace())
  70. this.log.trace('Parsing done: %j', this.json);
  71. return true;
  72. };
  73. LDAPMessage.prototype.toBer = function () {
  74. var writer = new BerWriter();
  75. writer.startSequence();
  76. writer.writeInt(this.messageID);
  77. writer.startSequence(this.protocolOp);
  78. if (this._toBer)
  79. writer = this._toBer(writer);
  80. writer.endSequence();
  81. if (this.controls && this.controls.length) {
  82. writer.startSequence(0xa0);
  83. this.controls.forEach(function (c) {
  84. c.toBer(writer);
  85. });
  86. writer.endSequence();
  87. }
  88. writer.endSequence();
  89. return writer.buffer;
  90. };
  91. ///--- Exports
  92. module.exports = LDAPMessage;