Library mqtt

An implementation of MQTT 3.1.1 https://docs.oasis-open.org/mqtt/mqtt/v3.1.1/os/mqtt-v3.1.1-os.html

This library does not currently implement the entire MQTT protocol, only those control packets which are necessary for existing scripts are included. Extending to accommodate additional control packets should not be difficult.

Author:

  • "Mak Kolybabi <mak@kolybabi.com>"

Copyright © Same as Nmap--See https://nmap.org/book/man-legal.html

Source: https://svn.nmap.org/nmap/nselib/mqtt.lua

Functions

Comm.build  (self, type, options)

Builds an MQTT control packet.

Comm.close (self)

Disconnects from the MQTT broker.

Comm.connect  (self, options)

Connects to the MQTT broker.

Comm.new  (self, host, port, options)

Creates a new Client instance.

Comm.packet_identifier See "2.3.1 Packet Identifier" section of the standard.  (self)

Generates a packet identifier.

Comm.parse  (self, buf, pos)

Parses an MQTT control packet.

Comm.receive  (self)

Receives an MQTT control packet.

Comm.send  (self, pkt)

Sends an MQTT control packet.

Helper.connect  (self, options)

Connects to the MQTT broker.

Helper.create  (self, host, port, opt)

Creates a new Helper instance.

Helper.receive  (self, types, timeout, res_type)

Listens for a response matching a list of types.

Helper.request  (self, req_type, options, res_type)

Sends a request to the MQTT broker, and receive a response.

Helper.send  (self, req_type, options)

Sends a request to the MQTT broker.

MQTT.fixed_header (num, flags, pkt)

Prefix the body of an MQTT packet with a fixed header.

MQTT.length_build (num)

Build a numeric field in MQTT's variable-length format.

MQTT.length_parse (buf, pos)

Parse a numeric field in MQTT's variable-length format.

MQTT.packet["CONNACK"].parse (fhflags, buf)

Parse an MQTT CONNACK control packet.

MQTT.packet["CONNECT"].build (options)

Build an MQTT CONNECT control packet.

MQTT.packet["DISCONNECT"].build (options)

Build an MQTT DISCONNECT control packet.

MQTT.packet["PUBLISH"].parse (fhflags, buf)

Parse an MQTT PUBLISH control packet.

MQTT.packet["SUBACK"].parse (fhflags, buf)

Parse an MQTT SUBACK control packet.

MQTT.packet["SUBSCRIBE"].build (options)

Build an MQTT SUBSCRIBE control packet.

MQTT.utf8_build (str)

Build a UTF-8 string in MQTT's length-prefixed format.

MQTT.utf8_parse (buf, pos)

Parse a UTF-8 string in MQTT's length-prefixed format.

Functions

Comm.build  (self, type, options)

Builds an MQTT control packet.

Parameters

self
 
type
Type of MQTT control packet to build.
options
Table of options accepted by the requested type of control packet.

Return values:

  1. status true on success, false on failure.
  2. response String representing a raw control packet on success, or containing the error message on failure.
Comm.close (self)

Disconnects from the MQTT broker.

Parameters

self
 
Comm.connect  (self, options)

Connects to the MQTT broker.

Parameters

self
 
options
 

Return values:

  1. status true on success, false on failure.
  2. err string containing the error message on failure.
Comm.new  (self, host, port, options)

Creates a new Client instance.

Parameters

self
 
host
Table as received by the action method.
port
Table as received by the action method.
options
Table as received by the action method.

Return value:

o Instance of Client.
Comm.packet_identifier See "2.3.1 Packet Identifier" section of the standard.  (self)

Generates a packet identifier.

Parameters

self
 

Return value:

Unique identifier for a packet.
Comm.parse  (self, buf, pos)

Parses an MQTT control packet.

Parameters

self
 
buf
String from which to parse the control packet.
pos
Position from which to start parsing.

Return values:

  1. pos String index on success, false on failure.
  2. response Table representing a control packet on success, string containing the error message on failure.
Comm.receive  (self)

Receives an MQTT control packet.

Parameters

self
 

Return values:

  1. status True on success, false on failure.
  2. response String representing a raw control packet on success, string containing the error message on failure.
Comm.send  (self, pkt)

Sends an MQTT control packet.

Parameters

self
 
pkt
String representing a raw control packet.

Return values:

  1. status true on success, false on failure.
  2. err string containing the error message on failure.
Helper.connect  (self, options)

Connects to the MQTT broker.

Parameters

self
 
options
Table of options for the CONNECT control packet.

Return values:

  1. status True on success, false on failure.
  2. response Table representing a CONNACK control packet on success, string containing the error message on failure.
Helper.create  (self, host, port, opt)

Creates a new Helper instance.

Parameters

self
 
host
Table as received by the action method.
port
Table as received by the action method.
opt
 

Return value:

o instance of Client
Helper.receive  (self, types, timeout, res_type)

Listens for a response matching a list of types.

Parameters

self
 
types
Type of control packet to build and send.
timeout
Number of seconds to listen for matching response, defaults to 5s.
res_type
Table of types of control packet to receive and parse.

Return values:

  1. status True on success, false on failure.
  2. response Table representing any res_type control packet on success, string containing the error message on failure.
Helper.request  (self, req_type, options, res_type)

Sends a request to the MQTT broker, and receive a response.

Parameters

self
 
req_type
Type of control packet to build and send.
options
Table of options for the request control packet.
res_type
Type of control packet to receive and parse.

Return values:

  1. status True on success, false on failure.
  2. response Table representing a res_type control packet on success, string containing the error message on failure.
Helper.send  (self, req_type, options)

Sends a request to the MQTT broker.

Parameters

self
 
req_type
Type of control packet to build and send.
options
Table of options for the request control packet.

Return values:

  1. status True on success, false on failure.
  2. err String containing the error message on failure.
MQTT.fixed_header (num, flags, pkt)

Prefix the body of an MQTT packet with a fixed header.

See section "2.2 Fixed header" of the standard.

Parameters

num
The type of the control packet.
flags
The flags of the control packet.
pkt
The string representing the control packet.

Return value:

A string representing a completed MQTT control packet.
MQTT.length_build (num)

Build a numeric field in MQTT's variable-length format.

See section "2.2.3 Remaining Length" of the standard.

Parameters

num
The value of the field.

Return value:

A variable-length field.
MQTT.length_parse (buf, pos)

Parse a numeric field in MQTT's variable-length format.

See section "2.2.3 Remaining Length" of the standard.

Parameters

buf
String from which to parse the numeric field.
pos
Position from which to start parsing.

Return values:

  1. pos String index on success, false on failure.
  2. response Parsed numeric field on success, string containing the error message on failure.
MQTT.packet["CONNACK"].parse (fhflags, buf)

Parse an MQTT CONNACK control packet.

See "3.2 CONNACK – Acknowledge connection request" section of the standard.

Parameters

fhflags
The flags of the control packet.
buf
The string representing the control packet.

Return values:

  1. status True on success, false on failure.
  2. response Table representing a CONNACK control packet on success, string containing the error message on failure.
MQTT.packet["CONNECT"].build (options)

Build an MQTT CONNECT control packet.

See "3.1 CONNECT – Client requests a connection to a Server" section of the standard.

Parameters

options
Table of options accepted by this type of control packet.

Return value:

A string representing a CONNECT control packet.
MQTT.packet["DISCONNECT"].build (options)

Build an MQTT DISCONNECT control packet.

See "3.14 DISCONNECT – Disconnect notification" section of the standard.

Parameters

options
Table of options accepted by this type of control packet.

Return value:

A string representing a DISCONNECT control packet.
MQTT.packet["PUBLISH"].parse (fhflags, buf)

Parse an MQTT PUBLISH control packet.

See "3.3 PUBLISH – Publish message" section of the standard.

Parameters

fhflags
The flags of the control packet.
buf
The string representing the control packet.

Return values:

  1. status True on success, false on failure.
  2. response Table representing a PUBLISH control packet on success, string containing the error message on failure.
MQTT.packet["SUBACK"].parse (fhflags, buf)

Parse an MQTT SUBACK control packet.

See "3.9 SUBACK – Subscribe acknowledgement" section of the standard.

Parameters

fhflags
The flags of the control packet.
buf
The string representing the control packet.

Return values:

  1. status True on success, false on failure.
  2. response Table representing a SUBACK control packet on success, string containing the error message on failure.
MQTT.packet["SUBSCRIBE"].build (options)

Build an MQTT SUBSCRIBE control packet.

See "3.8 SUBSCRIBE - Subscribe to topics" section of the standard.

Parameters

options
Table of options accepted by this type of control packet.

Return value:

A string representing a SUBSCRIBE control packet.
MQTT.utf8_build (str)

Build a UTF-8 string in MQTT's length-prefixed format.

See section "1.5.3 UTF-8 encoded strings" of the standard.

Parameters

str
The string to convert.

Return value:

A length-prefixed string.
MQTT.utf8_parse (buf, pos)

Parse a UTF-8 string in MQTT's length-prefixed format.

See section "1.5.3 UTF-8 encoded strings" of the standard.

Parameters

buf
The bytes to parse.
pos
The position from which to start parsing.

Return values:

  1. status True on success, false on failure.
  2. response Parsed string on success, string containing the error message on failure.