Skip to content
This repository has been archived by the owner on Nov 27, 2018. It is now read-only.

Add Marshal and LookupTemplateRecords functions #22

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

floren
Copy link

@floren floren commented Aug 23, 2018

I needed to unpack a Message, check which templates the data records referred to, attach those templates, and re-package the message as a binary IPFIX message. Here's a workflow:

	msg, _ := s.ParseBuffer(buf)
	templates, _ := s.LookupTemplateRecords(msg)
	msg.TemplateRecords = templates
	repacked, _ := s.Marshal(msg)

LookupTemplateRecords returns a []TemplateRecord representing all the templates referred to by the data records of the given Message. It returns an error if it doesn't have the specified templates available (hasn't seen a template record yet)

Marshal simply re-packs the given message as binary; you could for example receive a packet, parse it, modify it, then re-Marshal it and send it out to a final IPFIX collector.

@calmh
Copy link
Owner

calmh commented Aug 23, 2018

Neat

@floren
Copy link
Author

floren commented Sep 5, 2018

Checking in on this, is this functionality you're interested in? I think it's a useful addition for situations where you're not an endpoint, just a stop along the way to an eventual endpoint.

@calmh
Copy link
Owner

calmh commented Sep 5, 2018

Yes, I just forgot about it and didn't get around to review, sorry! At first I was worried that it was single purpose, but it looks like it can marshal pretty much all that we can unmarshal? One thing I reacted to on the initial pass was that it was all in one big function; but I didn't look at the details yet. It's on my todo list so I won't forget!

@floren
Copy link
Author

floren commented Sep 5, 2018

Yes, I apologize for the single giant function. It's a little ugly because I wanted to avoid allocations wherever possible; my first pass did something like 12 allocs per call to Marshall, while the current one only does 1 allocation. It's still not nearly as fast as the unpacking functions, but at least it's nicer to the allocator. Unfortunately this ended up with a giant function which tracks an offset into the buffer the whole way through.

@calmh
Copy link
Owner

calmh commented Sep 5, 2018

That's a valid reason; I'd probably like to explore whether it can be handled by passing around the buffer and maybe offset, maybe adding an allocation here and there if required for clarity... (But again, I didn't really stare at the code yet so maybe I'm talking out of my ass.)

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants