Library coap

An implementation of CoAP https://tools.ietf.org/html/rfc7252

This library does not currently implement the entire CoAP protocol, only those behaviours 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/coap.lua

Functions

COAP.build (options, payload)

Builds a CoAP message.

COAP.header.build (options)

Builds a CoAP message header.

COAP.header.codes.build (name)

Builds a CoAP message request or response code.

COAP.header.codes.parse (buf, pos)

Parses a CoAP request or response code.

COAP.header.find_option (hdr, name)

Finds the first instance of an option type in a header.

COAP.header.find_options (hdr, name, max)

Finds all instances of an option type in a header.

COAP.header.options.accept.build (val)

Builds a CoAP message header Accept option.

COAP.header.options.accept.parse (buf)

Parses a CoAP message header Accept option.

COAP.header.options.block.build (val)

Builds a CoAP message header Block option.

COAP.header.options.block.parse (buf)

Parses a CoAP message header Block option.

COAP.header.options.block1.build (val)

Builds a CoAP message header Block1 option.

COAP.header.options.block1.parse (buf)

Parses a CoAP message header Block1 option.

COAP.header.options.block2.build (val)

Builds a CoAP message header Block2 option.

COAP.header.options.block2.parse (buf)

Parses a CoAP message header Block2 option.

COAP.header.options.build (options)

Build CoAP message header options.

COAP.header.options.content_format.build (val)

Builds a CoAP message header Content-Format option.

COAP.header.options.content_format.parse (buf)

Parses a CoAP message header Content-Format option.

COAP.header.options.delta_length.build (delta, length)

Build the variable-length option delta and length field.

COAP.header.options.delta_length.parse (buf, pos)

Parse the variable-length option delta and length field.

COAP.header.options.etag.build (val)

Builds a CoAP message header ETag option.

COAP.header.options.etag.parse (buf)

Parses a CoAP message header ETag option.

COAP.header.options.if_match.build (val)

Builds a CoAP message header If-Match option.

COAP.header.options.if_match.parse (buf)

Parses a CoAP message header If-Match option.

COAP.header.options.if_none_match.build (val)

Builds a CoAP message header If-None-Match option.

COAP.header.options.if_none_match.parse (buf)

Parses a CoAP message header If-None-Match option.

COAP.header.options.location_path.build (val)

Builds a CoAP message header Location-Path option.

COAP.header.options.location_path.parse (buf)

Parses a CoAP message header Location-Path option.

COAP.header.options.location_query.build (val)

Builds a CoAP message header Location-Query option.

COAP.header.options.location_query.parse (buf)

Parses a CoAP message header Location-Query option.

COAP.header.options.max_age.build (val)

Builds a CoAP message header Max-Age option.

COAP.header.options.max_age.parse (buf)

Parses a CoAP message header Max-Age option.

COAP.header.options.parse (buf, pos)

Parses a CoAP message's header options.

COAP.header.options.proxy_scheme.build (val)

Builds a CoAP message header Proxy-Scheme option.

COAP.header.options.proxy_scheme.parse (buf)

Parses a CoAP message header Proxy-Scheme option.

COAP.header.options.proxy_uri.build (val)

Builds a CoAP message header Proxy-Uri option.

COAP.header.options.proxy_uri.parse (buf)

Parses a CoAP message header Proxy-Uri option.

COAP.header.options.Size1.build (val)

Builds a CoAP message header Size1 option.

COAP.header.options.size1.parse (buf)

Parses a CoAP message header Size1 option.

COAP.header.options.uri_host.build (val)

Builds a CoAP message header Uri-Host option.

COAP.header.options.uri_host.parse (buf)

Parses a CoAP message header Uri-Host option.

COAP.header.options.uri_path.build (val)

Builds a CoAP message header Uri-Path option.

COAP.header.options.uri_path.parse (buf)

Parses a CoAP message header Uri-Path option.

COAP.header.options.uri_port.build (val)

Builds a CoAP message header Uri-Port option.

COAP.header.options.uri_port.parse (buf)

Parses a CoAP message header Uri-Port option.

COAP.header.options.uri_query.build (val)

Builds a CoAP message header Uri-Query option.

COAP.header.options.uri_query.parse (buf)

Parses a CoAP message header Uri-Query option.

COAP.header.options.value.empty.parse (buf)

Parses a CoAP message Empty header option value.

COAP.header.options.value.empty.parse (buf)

Parses a CoAP message Empty header option value.

COAP.header.options.value.opaque.build (str)

Builds a CoAP message Opaque header option value.

COAP.header.options.value.opaque.parse (buf)

Parses a CoAP message Opaque header option value.

COAP.header.options.value.string.build (str)

Builds a CoAP message String header option value.

COAP.header.options.value.string.parse (buf)

Parses a CoAP message String header option value.

COAP.header.options.value.uint.build (val)

Builds a CoAP message Uint header option value.

COAP.header.options.value.uint.parse (buf)

Parses a CoAP message Uint header option value.

COAP.header.parse (buf, pos)

Parses a CoAP message header.

COAP.parse (buf, pos)

Parses a CoAP message.

COAP.payload.application_octet_stream.parse (hdr, buf)

Parse the Octet Stream payload of a CoAP message.

COAP.payload.application_xml.parse (hdr, buf)

Parse the XML payload of a CoAP message.

COAP.payload.exi.parse (hdr, buf)

Parse the EXI payload of a CoAP message.

COAP.payload.json.parse (hdr, buf)

Parse the JSON payload of a CoAP message.

COAP.payload.link_format.parse (hdr, buf)

Parse the Link Format payload of a CoAP message.

COAP.payload.parse (hdr, buf)

Parse the payload of a CoAP message.

COAP.payload.text_plain.parse (hdr, buf)

Parse the Plain Text payload of a CoAP message.

Comm.build (self, options, payload)

Builds a CoAP message.

Comm.close (self)

Disconnects from the CoAP endpoint.

Comm.connect (self, options)

Connects to the CoAP endpoint.

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

Creates a new Client instance.

Comm.parse (self, buf, pos)

Parses a CoAP message.

Comm.receive (self)

Receives an MQTT control packet.

Comm.send (self, pkt)

Sends a CoAP message.

Helper.connect (self, options)

Connects to the CoAP endpoint.

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

Creates a new Helper instance.

Helper.receive (self, ids, timeout)

Listens for a response matching a list of types.

Helper.request (self, options, payload)

Sends a request to the CoAP, and receive a response.

Helper.send (self, options, payload)

Sends a request to the CoAP endpoint.

Functions

COAP.build (options, payload)

Builds a CoAP message.

Parameters

options
Table of options accepted by the desired message build function.
payload
String representing the message payload.

Return values:

  1. status true on success, false on failure.
  2. response String representing a raw message on success, or containing the error message on failure.
COAP.header.build (options)

Builds a CoAP message header.

Parameters

options
Table of options accepted by the desired message build function.

Return values:

  1. status true on success, false on failure.
  2. response String representing a raw message header on success, or containing the error message on failure.
COAP.header.codes.build (name)

Builds a CoAP message request or response code.

Parameters

name
String naming the desired code.

Return values:

  1. status true on success, false on failure.
  2. response String representing a code on success, or containing the error message on failure.
COAP.header.codes.parse (buf, pos)

Parses a CoAP request or response code.

Parameters

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

Return values:

  1. pos String index on success, false on failure.
  2. response Table representing the code on success, string containing the error message on failure.
COAP.header.find_option (hdr, name)

Finds the first instance of an option type in a header.

Parameters

hdr
Table representing a message header.
name
String naming an option type.

Return value:

opt Table representing option on success, or nil if one was not found.

See also:

COAP.header.find_options (hdr, name, max)

Finds all instances of an option type in a header.

Parameters

hdr
Table representing a message header.
name
String naming an option type.
max
Maximum number of options to return.

Return value:

opts Table containing option all options found, may be empty.
COAP.header.options.accept.build (val)

Builds a CoAP message header Accept option.

Parameters

val
Number representing an acceptable content type.

Return value:

str String representing the option's value.
COAP.header.options.accept.parse (buf)

Parses a CoAP message header Accept option.

Parameters

buf
String from which to parse the option.

Return value:

val Number representing the option's value.
COAP.header.options.block.build (val)

Builds a CoAP message header Block option.

Parameters

val
Table representing the block's parameters.

Return value:

str String representing the option's value.

See also:

COAP.header.options.block.parse (buf)

Parses a CoAP message header Block option.

Parameters

buf
String from which to parse the option.

Return value:

val Table representing the option.

See also:

COAP.header.options.block1.build (val)

Builds a CoAP message header Block1 option.

Parameters

val
Table representing the option's parameters.

Return value:

str String representing the option's value.

See also:

COAP.header.options.block1.parse (buf)

Parses a CoAP message header Block1 option.

Parameters

buf
String from which to parse the option.

Return value:

response Table representing the option's value.

See also:

COAP.header.options.block2.build (val)

Builds a CoAP message header Block2 option.

Parameters

val
Table representing the option's parameters.

Return value:

str String representing the option.

See also:

COAP.header.options.block2.parse (buf)

Parses a CoAP message header Block2 option.

Parameters

buf
String from which to parse the option.

Return value:

response Table representing the option's value.

See also:

COAP.header.options.build (options)

Build CoAP message header options.

Parameters

options
Table of options and their values.

Return value:

response String representing a raw set of options, properly sorted.
COAP.header.options.content_format.build (val)

Builds a CoAP message header Content-Format option.

Parameters

val
Number representing the payload content format.

Return value:

str String representing the option's value.
COAP.header.options.content_format.parse (buf)

Parses a CoAP message header Content-Format option.

Parameters

buf
String from which to parse the option.

Return value:

val String representing the option's value.
COAP.header.options.delta_length.build (delta, length)

Build the variable-length option delta and length field.

Parameters

delta
Number representing the option ID's delta.
length
Number representing the length of the option's value.

Return value:

str String representing the delta and length fields.
COAP.header.options.delta_length.parse (buf, pos)

Parse the variable-length option delta and length field.

Parameters

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

Return values:

  1. pos Position at which parsing stopped on success, or false on failure.
  2. delta Delta value of the option's ID on success, or nil on failure.
  3. length Length of the option's value on success, or nil on failure.
  4. err nil on success, or an error message on failure.
COAP.header.options.etag.build (val)

Builds a CoAP message header ETag option.

Parameters

val
String representing the ETag's value.

Return value:

str String representing the option's value.
COAP.header.options.etag.parse (buf)

Parses a CoAP message header ETag option.

Parameters

buf
String from which to parse the option.

Return value:

val String representing the option's value.
COAP.header.options.if_match.build (val)

Builds a CoAP message header If-Match option.

Parameters

val
String representing the condition.

Return value:

str String representing the option's value.
COAP.header.options.if_match.parse (buf)

Parses a CoAP message header If-Match option.

Parameters

buf
String from which to parse the option.

Return value:

val String representing the option's value.
COAP.header.options.if_none_match.build (val)

Builds a CoAP message header If-None-Match option.

Parameters

val
Parameter is ignored, existing only to keep API consistent.

Return value:

str Empty string to keep API consistent.
COAP.header.options.if_none_match.parse (buf)

Parses a CoAP message header If-None-Match option.

Parameters

buf
Parameter is ignored, existing only to keep API consistent.

Return value:

val Nil due to the option being empty.
COAP.header.options.location_path.build (val)

Builds a CoAP message header Location-Path option.

Parameters

val
String representing a path.

Return value:

str String representing the option's value.
COAP.header.options.location_path.parse (buf)

Parses a CoAP message header Location-Path option.

Parameters

buf
String from which to parse the option.

Return value:

val String representing the option's value.
COAP.header.options.location_query.build (val)

Builds a CoAP message header Location-Query option.

Parameters

val
String representing the query.

Return value:

str String representing the option's value.
COAP.header.options.location_query.parse (buf)

Parses a CoAP message header Location-Query option.

Parameters

buf
String from which to parse the option.

Return value:

val String representing the option's value.
COAP.header.options.max_age.build (val)

Builds a CoAP message header Max-Age option.

Parameters

val
Number representing the maximum age.

Return value:

str String representing the option's value
COAP.header.options.max_age.parse (buf)

Parses a CoAP message header Max-Age option.

Parameters

buf
String from which to parse the option.

Return value:

val Number representing the option's value.
COAP.header.options.parse (buf, pos)

Parses a CoAP message's header options.

Parameters

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

Return values:

  1. pos String index on success, false on failure.
  2. response Table representing options on success, string containing the error message on failure.
COAP.header.options.proxy_scheme.build (val)

Builds a CoAP message header Proxy-Scheme option.

Parameters

val
String representing the proxy scheme.

Return value:

str String representing the option's value.
COAP.header.options.proxy_scheme.parse (buf)

Parses a CoAP message header Proxy-Scheme option.

Parameters

buf
String from which to parse the option.

Return value:

val String representing the option's value.
COAP.header.options.proxy_uri.build (val)

Builds a CoAP message header Proxy-Uri option.

Parameters

val
String representing the proxy URI.

Return value:

str String representing the option's value.
COAP.header.options.proxy_uri.parse (buf)

Parses a CoAP message header Proxy-Uri option.

Parameters

buf
String from which to parse the option.

Return value:

val String representing the option's value.
COAP.header.options.Size1.build (val)

Builds a CoAP message header Size1 option.

Parameters

val
Number representing a size.

Return value:

str String representing the option's value.
COAP.header.options.size1.parse (buf)

Parses a CoAP message header Size1 option.

Parameters

buf
String from which to parse the option.

Return value:

val Number representing the option's value.
COAP.header.options.uri_host.build (val)

Builds a CoAP message header Uri-Host option.

Parameters

val
String representing the host of the URI.

Return value:

str String representing the option's value.
COAP.header.options.uri_host.parse (buf)

Parses a CoAP message header Uri-Host option.

Parameters

buf
String from which to parse the option.

Return value:

val String representing the option's value.
COAP.header.options.uri_path.build (val)

Builds a CoAP message header Uri-Path option.

Parameters

val
String representing a path in the URI.

Return value:

str String representing the option's value.
COAP.header.options.uri_path.parse (buf)

Parses a CoAP message header Uri-Path option.

Parameters

buf
String from which to parse the option.

Return value:

val String representing the option's value.
COAP.header.options.uri_port.build (val)

Builds a CoAP message header Uri-Port option.

Parameters

val
Number representing an endpoint's port number.

Return value:

str String representing the option's value.
COAP.header.options.uri_port.parse (buf)

Parses a CoAP message header Uri-Port option.

Parameters

buf
String from which to parse the option.

Return value:

val Number representing the option's value.
COAP.header.options.uri_query.build (val)

Builds a CoAP message header Uri-Query option.

Parameters

val
String representing a query string in the URI.

Return value:

str String representing the option's value.
COAP.header.options.uri_query.parse (buf)

Parses a CoAP message header Uri-Query option.

Parameters

buf
String from which to parse the option.

Return value:

val String representing the option's value.
COAP.header.options.value.empty.parse (buf)

Parses a CoAP message Empty header option value.

Parameters

buf
Parameter is ignored, existing only to keep API consistent.

Return value:

val Nil due to the option being empty.
COAP.header.options.value.empty.parse (buf)

Parses a CoAP message Empty header option value.

Parameters

buf
Parameter is ignored, existing only to keep API consistent.

Return value:

val Nil due to the option being empty.
COAP.header.options.value.opaque.build (str)

Builds a CoAP message Opaque header option value.

Parameters

str
String representing an opaque option value.

Return value:

str String representing the option's value.
COAP.header.options.value.opaque.parse (buf)

Parses a CoAP message Opaque header option value.

Parameters

buf
String from which to parse the option.

Return value:

val String representing the option's value.
COAP.header.options.value.string.build (str)

Builds a CoAP message String header option value.

Parameters

str
String representing a string option value.

Return value:

str String representing the option's value.
COAP.header.options.value.string.parse (buf)

Parses a CoAP message String header option value.

Parameters

buf
String from which to parse the option.

Return value:

val String representing the option's value.
COAP.header.options.value.uint.build (val)

Builds a CoAP message Uint header option value.

Parameters

val
Number representing a Uint option value.

Return value:

str String representing the option's value.
COAP.header.options.value.uint.parse (buf)

Parses a CoAP message Uint header option value.

Parameters

buf
String from which to parse the option.

Return value:

val Number representing the option's value.
COAP.header.parse (buf, pos)

Parses a CoAP message header.

Parameters

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

Return values:

  1. pos String index on success, false on failure.
  2. response Table representing a message header on success, string containing the error message on failure.
COAP.parse (buf, pos)

Parses a CoAP message.

Parameters

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

Return values:

  1. pos String index on success, false on failure.
  2. response Table representing a message on success, string containing the error message on failure.
COAP.payload.application_octet_stream.parse (hdr, buf)

Parse the Octet Stream payload of a CoAP message.

Parameters

hdr
Table representing a message header.
buf
String from which to parse the payload.

Return values:

  1. status True on success, false on failure.
  2. val String containing parsed payload on success, string containing the error message on failure.
COAP.payload.application_xml.parse (hdr, buf)

Parse the XML payload of a CoAP message.

Parameters

hdr
Table representing a message header.
buf
String from which to parse the payload.

Return values:

  1. status True on success, false on failure.
  2. response Object containing parsed payload on success, string containing the error message on failure.
COAP.payload.exi.parse (hdr, buf)

Parse the EXI payload of a CoAP message.

Parameters

hdr
Table representing a message header.
buf
String from which to parse the payload.

Return values:

  1. status True on success, false on failure.
  2. response Object containing parsed payload on success, string containing the error message on failure.
COAP.payload.json.parse (hdr, buf)

Parse the JSON payload of a CoAP message.

Parameters

hdr
Table representing a message header.
buf
String from which to parse the payload.

Return values:

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

Parse the Link Format payload of a CoAP message.

Parameters

hdr
Table representing a message header.
buf
String from which to parse the payload.

Return values:

  1. status True on success, false on failure.
  2. val Table containing parsed payload on success, string containing the error message on failure.
COAP.payload.parse (hdr, buf)

Parse the payload of a CoAP message.

Parameters

hdr
Table representing a message header.
buf
String from which to parse the payload.

Return values:

  1. status True on success, false on failure.
  2. val Object containing parsed payload on success, string containing the error message on failure.
COAP.payload.text_plain.parse (hdr, buf)

Parse the Plain Text payload of a CoAP message.

Parameters

hdr
Table representing a message header.
buf
String from which to parse the payload.

Return values:

  1. status True on success, false on failure.
  2. val String containing parsed payload on success, string containing the error message on failure.
Comm.build (self, options, payload)

Builds a CoAP message.

Parameters

self
 
options
Table of options accepted by the requested type of message.
payload
 

Return values:

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

Disconnects from the CoAP endpoint.

Parameters

self
 
Comm.connect (self, options)

Connects to the CoAP endpoint.

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
String as received by the action method.
port
Number as received by the action method.
options
Table as received by the action method.

Return value:

o Instance of Client.
Comm.parse (self, buf, pos)

Parses a CoAP message.

Parameters

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

Return values:

  1. pos String index on success, false on failure.
  2. response Table representing a CoAP message 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 message on success, string containing the error message on failure.
Comm.send (self, pkt)

Sends a CoAP message.

Parameters

self
 
pkt
String representing a raw message.

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 CoAP endpoint.

Parameters

self
 
options
Table of options for the initial message.

Return values:

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

Creates a new Helper instance.

Parameters

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

Return value:

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

Listens for a response matching a list of types.

Parameters

self
 
ids
Table of message IDs to wait for.
timeout
Number of seconds to listen for matching response, defaults to 5s.

Return values:

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

Sends a request to the CoAP, and receive a response.

Parameters

self
 
options
Table of options for the message.
payload
String containing the message body.

Return values:

  1. status True on success, false on failure.
  2. response Table representing a message with the corresponding message ID on success, string containing the error message on failure.
Helper.send (self, options, payload)

Sends a request to the CoAP endpoint.

Parameters

self
 
options
Table of options for the message.
payload
Payload of message.

Return values:

  1. status True on success, false on failure.
  2. err String containing the error message on failure.