Back to Ntopng

ntopng Lua API Reference (`flow.*` bindings)

doc/README.lua_flow_api.md

6.610.0 KB
Original Source

ntopng Lua API Reference (flow.* bindings)

This document describes all C→Lua bindings exposed as flow.* functions via src/LuaEngineFlow.cpp. It is intended both for human developers writing Lua scripts and as a machine-readable reference for AI-assisted code generation (e.g. Claude Code in this repository).


Table of Contents

  1. How flow.* works
  2. Usage context
  3. Endpoints & Addresses
  4. Traffic Counters
  5. Protocol Classification
  6. Flow Characteristics
  7. L7 Protocol Metadata
  8. Alert Triggering
  9. Complete Function Index

1. How flow.* works

Context

flow.* functions are available only inside flow check scripts — Lua scripts that are invoked by the ntopng flow-processing engine for each active flow. They operate on the current flow implicitly stored in the Lua VM context (NtopngLuaContext::flow); no arguments are needed to identify the flow.

C→Lua registration

Every function in _ntop_flow_reg[] (bottom of src/LuaEngineFlow.cpp) follows this pattern:

c
static int ntop_flow_get_xxx(lua_State* vm) {
    NtopngLuaContext* c = getLuaVMContext(vm);
    Flow* f = c ? c->flow : NULL;

    if (f)
        lua_push<type>(vm, f->get_xxx());
    else
        lua_pushnil(vm);

    return ntop_lua_return_value(vm, __FUNCTION__, CONST_LUA_OK);
}

// Registered as:
{ "xxx", ntop_flow_get_xxx },   // called as flow.xxx()

Where flow check scripts live

Flow check scripts are stored in:

scripts/lua/modules/flow_checks/

Each script is a Lua module that exports a check() function called per-flow by the engine.


2. Usage context

Standard boilerplate for a flow check script

lua
-- scripts/lua/modules/flow_checks/my_flow_check.lua
local dirs = ntop.getDirs()
package.path = dirs.installdir .. "/scripts/lua/modules/?.lua;" .. package.path

local flow_consts = require("flow_consts")

local my_check = {}

-- Called once per active flow by the engine
function my_check.check(flow_info)
    local cli   = flow.cli()          -- client IP string
    local srv   = flow.srv()          -- server IP string
    local proto = flow.protocol()     -- L4 protocol number
    local bytes = flow.bytes()        -- total bytes

    if bytes > 1000000 then
        flow.triggerAlert(flow_consts.alert_types.flow_score, "Large flow detected")
    end
end

return my_check

Important notes

  • flow.* functions return nil if called outside a flow check context (i.e. when NtopngLuaContext::flow is NULL).
  • All functions take no arguments (except flow.triggerAlert).
  • flow.* is completely separate from interface.* — there is no need to call interface.select() inside flow check scripts.

3. Endpoints & Addresses

Lua callReturnsDescription
flow.cli()stringThe client (connection initiator) IP address, e.g. "192.168.1.10" or "2001:db8::1". Returns nil if the flow has no client IP.
flow.cli_port()integerThe client TCP/UDP source port number.
flow.srv()stringThe server (connection responder) IP address. Returns nil if the flow has no server IP.
flow.srv_port()integerThe server TCP/UDP destination port number.
flow.vlan_id()integerThe VLAN ID associated with this flow. Returns 0 if the flow is not VLAN-tagged.

4. Traffic Counters

Lua callReturnsDescription
flow.bytes()integerTotal bytes transferred in both directions (cli→srv + srv→cli).
flow.cli2srv_bytes()integerBytes transferred from client to server only.
flow.srv2cli_bytes()integerBytes transferred from server to client only.

5. Protocol Classification

L4 (transport) protocol

Lua callReturnsDescription
flow.protocol()integerThe L4 protocol number per IANA assignments. Common values: 6 (TCP), 17 (UDP), 1 (ICMP), 58 (ICMPv6), 132 (SCTP).

L7 (application) protocol — nDPI IDs

nDPI classifies flows hierarchically as master_protocol.app_protocol (e.g. HTTP.Facebook). The two IDs are separate:

Lua callReturnsDescription
flow.l7_master_proto()integerThe nDPI master (encapsulating/transport) protocol ID — e.g. the HTTP protocol ID for HTTP.Facebook. Use interface.getnDPIProtoName() to convert to a string.
flow.l7_proto()integerThe nDPI application protocol ID — e.g. the Facebook protocol ID for HTTP.Facebook.
flow.l7_proto_name()stringThe full human-readable nDPI protocol name string, e.g. "HTTP.Facebook" or "TLS.Google". Combines master and app protocol.

6. Flow Characteristics

Lua callReturnsDescription
flow.direction()stringThe locality direction of this flow. One of: "local@local" (both endpoints are local), "local@remote" (client local, server remote), "remote@local" (client remote, server local), "remote2remote" (neither endpoint is local), or "unknown".
flow.is_oneway()booleantrue if only one direction of the flow has seen traffic (no reply packets observed). Useful for detecting port scans or half-open connections.
flow.is_unicast()booleantrue if both the client and server IP addresses are unicast (not broadcast or multicast).

7. L7 Protocol Metadata

These functions return dissected protocol-specific metadata tables. The returned table fields are populated only when nDPI has fully dissected the protocol for this flow; fields may be nil if the flow is still being classified or if the protocol does not apply.

flow.http() — HTTP metadata

Returns a table with HTTP flow details. Common fields:

FieldTypeDescription
methodstringHTTP request method ("GET", "POST", etc.)
urlstringFull request URL
content_typestringHTTP Content-Type header value
return_codeintegerHTTP response status code (e.g. 200, 404)
server_namestringHTTP Host header value

flow.dns() — DNS metadata

Returns a table with DNS flow details. Common fields:

FieldTypeDescription
last_querystringThe queried domain name
last_query_typeintegerDNS query type code (e.g. 1=A, 28=AAAA, 5=CNAME)
last_return_codeintegerDNS response code (0=NOERROR, 3=NXDOMAIN, etc.)
replies_as_stringstringComma-separated list of reply IP addresses
invalid_chars_in_querybooleantrue if the query contains suspicious non-printable characters

flow.ssh() — SSH metadata

Returns a table with SSH flow details. Common fields:

FieldTypeDescription
client_signaturestringSSH client version string
server_signaturestringSSH server version string
hassh_clientstringHASSH fingerprint of the SSH client
hassh_serverstringHASSH fingerprint of the SSH server

flow.tls_quic() — TLS/QUIC metadata

Returns a table with TLS or QUIC flow details. Common fields:

FieldTypeDescription
client_requested_server_namestringSNI (Server Name Indication) from the ClientHello
server_namesstringCertificate subject/SAN names from the server certificate
issuerDNstringCertificate issuer distinguished name
subjectDNstringCertificate subject distinguished name
ja3_clientstringJA3 TLS client fingerprint
ja3_serverstringJA3S TLS server fingerprint
notBeforeintegerCertificate validity start (Unix timestamp)
notAfterintegerCertificate validity end (Unix timestamp)
unsafe_cipherbooleantrue if an unsafe/deprecated cipher suite was negotiated

8. Alert Triggering

Lua callReturnsDescription
flow.triggerAlert(value, msg)nilTriggers a custom alert on the current flow. value is a numeric severity/score value; msg is a descriptive string that will appear in the alert details. Has no effect if called outside a flow check context (when flow is nil).

Example

lua
-- Detect large DNS responses (possible DNS tunneling)
local dns = flow.dns()
if dns and dns.last_return_code == 0 then
    local srv2cli = flow.srv2cli_bytes()
    if srv2cli > 512 then
        flow.triggerAlert(50, "Large DNS response: " .. tostring(srv2cli) .. " bytes")
    end
end

9. Complete Function Index

All 20 flow.* functions:

Lua functionC implementationCategory
flow.bytes()ntop_flow_get_bytesTraffic
flow.cli()ntop_flow_get_clientEndpoints
flow.cli_port()ntop_flow_get_client_portEndpoints
flow.cli2srv_bytes()ntop_flow_get_cli2srv_bytesTraffic
flow.direction()ntop_flow_get_directionCharacteristics
flow.dns()ntop_flow_get_l7_proto_dnsL7 metadata
flow.http()ntop_flow_get_l7_proto_httpL7 metadata
flow.is_oneway()ntop_flow_is_onewayCharacteristics
flow.is_unicast()ntop_flow_is_unicastCharacteristics
flow.l7_master_proto()ntop_flow_get_l7_master_protoProtocol
flow.l7_proto()ntop_flow_get_l7_protoProtocol
flow.l7_proto_name()ntop_flow_get_l7_proto_nameProtocol
flow.protocol()ntop_flow_get_protocolProtocol
flow.ssh()ntop_flow_get_l7_proto_sshL7 metadata
flow.srv()ntop_flow_get_serverEndpoints
flow.srv_port()ntop_flow_get_server_portEndpoints
flow.srv2cli_bytes()ntop_flow_get_srv2cli_bytesTraffic
flow.tls_quic()ntop_flow_get_l7_proto_tls_quicL7 metadata
flow.triggerAlert(value, msg)ntop_trigger_flow_alertAlerts
flow.vlan_id()ntop_flow_get_vlan_idEndpoints