You will probably need functionality that will always be closely related to package dissection. Good protocol dissectors are really necessary to extract the necessary information. So my suggestion is to use the best open source tool available - wireshark.org
It provides the functionality of "Follow TCP stream":

It doesn't seem like you can easily extract some of the Wireshark dissection logic, but at least there is a good example of packet-tcp
typedef struct _tcp_flow_t { guint32 base_seq; tcp_unacked_t *segments; guint32 fin; guint32 lastack; nstime_t lastacktime; guint32 lastnondupack; guint32 dupacknum; guint32 nextseq; guint32 maxseqtobeacked; guint32 nextseqframe;
Basically, there is a separate logic for extracting logic; pay attention to find_conversation usage :
void add_tcp_process_info(guint32 frame_num, address *local_addr, address *remote_addr, guint16 local_port, guint16 remote_port, guint32 uid, guint32 pid, gchar *username, gchar *command) { conversation_t *conv; struct tcp_analysis *tcpd; tcp_flow_t *flow = NULL; conv = find_conversation(frame_num, local_addr, remote_addr, PT_TCP, local_port, remote_port, 0); if (!conv) { return; }
Actual logic is well documented and available here :
conversation_t * find_conversation(const guint32 frame_num, const address *addr_a, const address *addr_b, const port_type ptype, const guint32 port_a, const guint32 port_b, const guint options) { conversation_t *conversation; if (!(options & (NO_ADDR_B|NO_PORT_B))) {
So I actually suggest using the EPAN library . You can extract this library and use it yourself. Be careful with the license.