Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

multiple data set #3

Open
fedy85 opened this issue Jun 30, 2023 · 3 comments
Open

multiple data set #3

fedy85 opened this issue Jun 30, 2023 · 3 comments

Comments

@fedy85
Copy link

fedy85 commented Jun 30, 2023

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
Copy link
Owner

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

@fedy85
Copy link
Author

fedy85 commented Jul 2, 2023 via email

@GabrielBuragev
Copy link
Owner

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

Repository owner deleted a comment from Pappyskull1 Feb 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants