1
0
mirror of https://github.com/netsampler/goflow2.git synced 2024-05-06 15:54:52 +00:00

goflow2: allows using a single port for multiple protocols (sFlow, NetFlow, IPFIX) (#197)

This commit is contained in:
Louis
2023-08-14 18:39:56 -07:00
committed by GitHub
parent a248b2d458
commit b047aafe15
2 changed files with 42 additions and 1 deletions

View File

@ -231,6 +231,8 @@ func main() {
p = utils.NewSFlowPipe(cfgPipe)
} else if listenAddrUrl.Scheme == "netflow" {
p = utils.NewNetFlowPipe(cfgPipe)
} else if listenAddrUrl.Scheme == "flow" {
p = utils.NewFlowPipe(cfgPipe)
} else {
l.Errorf("scheme %s does not exist", listenAddrUrl.Scheme)
return

View File

@ -186,7 +186,7 @@ func (p *NetFlowPipe) DecodeFlow(msg interface{}) error {
return &PipeMessageError{pkt, err}
}
default:
return &PipeMessageError{pkt, fmt.Errorf("Not a NetFlow packet")}
return &PipeMessageError{pkt, fmt.Errorf("not a NetFlow packet")}
}
var flowMessageSet []producer.ProducerMessage
@ -222,3 +222,42 @@ func (p *NetFlowPipe) DecodeFlow(msg interface{}) error {
func (p *NetFlowPipe) Close() {
}
type AutoFlowPipe struct {
*SFlowPipe
*NetFlowPipe
}
func NewFlowPipe(cfg *PipeConfig) *AutoFlowPipe {
p := &AutoFlowPipe{
SFlowPipe: NewSFlowPipe(cfg),
NetFlowPipe: NewNetFlowPipe(cfg),
}
return p
}
func (p *AutoFlowPipe) Close() {
p.SFlowPipe.Close()
p.NetFlowPipe.Close()
}
func (p *AutoFlowPipe) DecodeFlow(msg interface{}) error {
pkt, ok := msg.(*Message)
if !ok {
return fmt.Errorf("flow is not *Message")
}
buf := bytes.NewBuffer(pkt.Payload)
var proto uint32
if err := utils.BinaryDecoder(buf, &proto); err != nil {
return &PipeMessageError{pkt, err}
}
protoNetFlow := (proto & 0xFFFF0000) >> 16
if proto == 5 {
return p.SFlowPipe.DecodeFlow(msg)
} else if protoNetFlow == 5 || protoNetFlow == 9 || protoNetFlow == 10 {
return p.NetFlowPipe.DecodeFlow(msg)
}
return fmt.Errorf("could not identify protocol %d", proto)
}