func()

in pcap-cli/internal/transformer/flow_mutex.go [202:250]


func (fm *flowMutex) getTracedFlow(
	flowID *uint64,
	seq, ack *uint32,
	local bool,
) (TracedFlowProvider, bool) {
	ref := seq
	if local {
		ref = ack
	}

	streamToSequenceMap, ok := fm.flowToStreamToSequenceMap.Get(*flowID)

	// no HTTP/1.1 request with a `traceID` has been seen for this `flowID`
	if !ok { // it is also possible that packet for HTTP request for this `flowID`
		return func(_ *uint32) (*TracedFlow, bool) { return nil, false }, false
	}

	// [ToDo]: memoize stream to trace mapping
	return func(stream *uint32) (*TracedFlow, bool) {
		// an HTTP/1.1 request with a `traceID` has already been seen for this `flowID`
		var tracedFlow, lastTracedFlow *TracedFlow = nil, nil
		if sttsm, ok := streamToSequenceMap.Get(*stream); ok {
			sttsm.Range(func(r uint32, tf *TracedFlow) bool {
				// Loop over the map keys (ascending sequence numbers) until one greater than `sequence` is found.
				// HTTP/1.1 is not multiplexed, so a new request using the same TCP connection ( i/e: pooling )
				// should be observed (alongside its `traceID`) with a higher sequence number than the previous one;
				// when the key (a sequence number) is greater than the current one, stop looping;
				// the previously analyzed `key` (sequence number) must be pointing to the correct `traceID`.
				// TL;DR: `traceID`s exist within a specific TCP sequence range, which configures a boundary.
				if *ref > r {
					tracedFlow = tf
				}
				lastTracedFlow = tf
				return true
			})

			// TCP sequence number is `uint32` so it is possible
			// for for it to be rolled over if it gets too big.
			// In such case `sequence` was not greater than any `key` in the map,
			// so the last visited `key` might be pointing to the correct `traceID`
			if tracedFlow == nil {
				tracedFlow = lastTracedFlow
			}

			return tracedFlow, true
		}
		return nil, false
	}, true
}