github.com/zartbot/goflow@v0.0.0-20221001080520-456f7dfad288/service/flowinfo/main.go (about)

     1  package flowinfo
     2  
     3  import (
     4  	"hash/fnv"
     5  )
     6  
     7  func UpdateFlowInfo(d map[string]interface{}, EnableNAT bool) {
     8  	var isFlowRecord, direction bool
     9  
    10  	NormalizeForwardingStats(d)
    11  	//flowinfo lookup
    12  	flowrecord, isFlowRecord := SrcDstLookUp(d, EnableNAT)
    13  	if isFlowRecord == false {
    14  		flowrecord, _ = BidiLookUp(d, EnableNAT)
    15  	}
    16  
    17  	//flow direction
    18  	bidirectionT, bDirectionValid := d["biflowDirection"]
    19  	flowdirectionT, fDirectionValid := d["flowDirection"]
    20  	if fDirectionValid && flowdirectionT != nil {
    21  		if flowdirectionT.(uint8) == 0 {
    22  			//FlowDirection is INPUT
    23  			/*-----OUT-----^observationPoint^|==Device==|---IN------*/
    24  			/*-----OUT-----(inputInterface)>>>>>>>>>>>>>>---IN------*/
    25  			direction = true
    26  			flowrecord.IPAddress_A, flowrecord.IPAddress_B = flowrecord.IPAddress_B, flowrecord.IPAddress_A
    27  			flowrecord.Port_A, flowrecord.Port_B = flowrecord.Port_B, flowrecord.Port_A
    28  			flowrecord.Direction = !flowrecord.Direction
    29  			if isFlowRecord {
    30  				d["out2in_bytes"] = d["total_bytes"]
    31  				d["out2in_pkts"] = d["total_pkts"]
    32  			}
    33  		} else {
    34  			//FlowDirection is OUTPUT
    35  			/*-----OUT-----^observationPoint^|==Device==|---IN------*/
    36  			/*-----OUT-----(egressIntf)<<<<<<<<<<<<<<<<<<---IN------*/
    37  			if isFlowRecord {
    38  				d["in2out_bytes"] = d["total_bytes"]
    39  				d["in2out_pkts"] = d["total_pkts"]
    40  			}
    41  		}
    42  		if _, ValidObvid := d["observationPointId"]; ValidObvid == false {
    43  			//add Observable Point Name
    44  			if direction {
    45  				//FlowDirection is INPUT
    46  				d["observationInterface"] = d["ingressInterfaceName"]
    47  				d["observationPointId"] = d["ingressInterface"]
    48  				d["observationFlowDirection"] = "input"
    49  			} else {
    50  				d["observationInterface"] = d["egressInterfaceName"]
    51  				d["observationPointId"] = d["egressInterface"]
    52  				d["observationFlowDirection"] = "output"
    53  			}
    54  		}
    55  	}
    56  
    57  	if bDirectionValid && bidirectionT != nil {
    58  		// this is a very tricky detection since Router currently only report 0,1,2
    59  		// all biflowDirection == 0 flow has src=0.0.0.0 , dst=0.0.0.0 ...
    60  		if bidirectionT.(uint8) == 2 {
    61  			flowrecord.IPAddress_A, flowrecord.IPAddress_B = flowrecord.IPAddress_B, flowrecord.IPAddress_A
    62  			flowrecord.Port_A, flowrecord.Port_B = flowrecord.Port_B, flowrecord.Port_A
    63  			flowrecord.Direction = !flowrecord.Direction
    64  		}
    65  	}
    66  
    67  	if (flowrecord.IPAddress_A != nil) && (flowrecord.IPAddress_B != nil) {
    68  		h32a := fnv.New32a()
    69  		h32b := fnv.New32a()
    70  		h32a.Write(flowrecord.IPAddress_A)
    71  		h32b.Write(flowrecord.IPAddress_B)
    72  		flowrecord.Hash_A = h32a.Sum32()
    73  		flowrecord.Hash_B = h32b.Sum32()
    74  		flowrecord.Hash_A_Mod = h32a.Sum32() % 10000
    75  		flowrecord.Hash_B_Mod = h32b.Sum32() % 10000
    76  
    77  		d["flowinfo"] = flowrecord
    78  
    79  	}
    80  }