GabrielBuragev / ipfix_node

Ipfix flow deserializer for Node.js
2 stars 1 forks source link

multiple data set #3

Open fedy85 opened 1 year ago

fedy85 commented 1 year ago

The library accepts only one flow in a packet. But IPFIX packet contains more than one. I've made a fix. Could you merge it into the master ?

/ipfix_node/lib/ipfix_packet_elements/data_set/record.js

--- record.js.backup    2023-06-30 13:38:51.447062387 +0200
+++ record.js   2023-06-30 13:42:54.227860127 +0200
@@ -4,29 +4,22 @@
     this.dataSetType = undefined;
     this.Fields = [];
     var template = params.template;
+    this.byteLength = 0;

     var self = this;

     var _construct = function() {
-        var i = 0;
-        self.templateId = buffer.readUInt16BE(i);
-        //Prepare buffer for the record
-        buffer = buffer.slice(4);
-
-        var currentField = 0;
-
         var CachedFieldSpecifiers = template.FieldSpecifiers;
         var cfsLength = Object.keys(CachedFieldSpecifiers).length
         var currentField = 0;
-
         while (currentField < cfsLength) {
             var cachedField = CachedFieldSpecifiers[currentField];
             var FreshField = new FieldSpecifier(buffer, cachedField, params);
             self.Fields.push(FreshField);
+            self.byteLength += FreshField.sizeInBytes;
             buffer = buffer.slice(FreshField.sizeInBytes);
             currentField++;
         }
-
         return this;
     };

and

/ipfix_node/lib/ipfix_packet_elements/ipfix_packet.js (contains changes from my previous issue with multiple templates)

--- ipfix_packet.js.backup      2023-06-30 13:47:43.396803003 +0200
+++ ipfix_packet.js     2023-06-30 13:47:20.540728681 +0200
@@ -18,14 +18,20 @@
         self.Header = new IpfixHeader(buffer);
         buffer = buffer.slice(self.Header.sizeInBytes);
         while (buffer.byteLength > 0) {
+
             var setId = buffer.readUInt16BE(0);
             var setType = getSetType(setId);
             var sizeInBytes = buffer.readUInt16BE(2);
-            if (setType == 'DataTemplate') {
-                var TemplateSet = new TemplateRecord(buffer);

-                storeTemplate(TemplateSet);
-                self.TemplateSets.push(TemplateSet);
+            if (setType == 'DataTemplate') {
+                var isLast = false;
+                while (!isLast) {
+                    var TemplateSet = new TemplateRecord(buffer);
+                    buffer = TemplateSet.newBuffer;
+                    isLast = TemplateSet.isLast;
+                    storeTemplate(TemplateSet);
+                    self.TemplateSets.push(TemplateSet);
+                }
             } else if (setType == 'OptionTemplate') {
                 var OptionsTemplateSet = new OptionsTemplateRecord(buffer);
                 storeTemplate(OptionsTemplateSet);
@@ -57,15 +63,20 @@
                 if(typeof template == 'undefined')
                     return console.error(`Couldn't fetch template with id ${templateNeeded.id}`)
                 try{
-                    var DataSet = new DataSetRecord(buffer,{template: template, parseDataValues: params.parseDataValues});
-                    self.DataSets.push(DataSet);
+                    var templateId = buffer.readUInt16BE(0);
+                    buffer = buffer.slice(4);
+                    while (buffer.byteLength > 1) {     // padding byte at the end
+                        var DataSet = new DataSetRecord(buffer,{template: template, parseDataValues: params.parseDataValues});
+                        DataSet.templateId = templateId;
+                        self.DataSets.push(DataSet);
+                        buffer = buffer.slice(DataSet.byteLength);
+                    }
                 }catch(e){
                     console.error(e);
                     console.error(`Couldn't deserialize template with id ${templateNeeded.id}`);
                     console.error(`Template with id ${templateNeeded.id} has invalid structure !`);
                 }
             }
-
             buffer = buffer.slice(sizeInBytes);
         }
     };
GabrielBuragev commented 1 year ago

Hey @fedy85, thank you for this! Could you please open a PR so we can merge this?

fedy85 commented 1 year ago

Hi,

I'am not much experienced with git. What PR Stand for, pleas ?

Thomas

Dne pá 30. 6. 2023 14:20 uživatel legape @.***> napsal:

Hey @fedy85 https://github.com/fedy85, thank you for this! Could you please open a PR so we can merge this?

— Reply to this email directly, view it on GitHub https://github.com/GabrielBuragev/ipfix_node/issues/3#issuecomment-1614574187, or unsubscribe https://github.com/notifications/unsubscribe-auth/AONW3JGDRYDSEAN3ADSSGATXN3AALANCNFSM6AAAAAAZZX2WN4 . You are receiving this because you were mentioned.Message ID: @.***>

GabrielBuragev commented 1 year ago

Hey @fedy85. Here you can find pretty much everything about the whole flow or forking and creating a PR (Pull-Request) against my repository: https://gist.github.com/Chaser324/ce0505fbed06b947d962