Constrained Application Protocol

Last updated

Constrained Application Protocol (CoAP) is a specialized UDP-based Internet application protocol for constrained devices, as defined in RFC 7252. It enables those constrained devices called "nodes" to communicate with the wider Internet using similar protocols. CoAP is designed for use between devices on the same constrained network (e.g., low-power, lossy networks), between devices and general nodes on the Internet, and between devices on different constrained networks both joined by an internet. CoAP is also being used via other mechanisms, such as SMS on mobile communication networks.

Contents

CoAP is an application-layer protocol that is intended for use in resource-constrained Internet devices, such as wireless sensor network nodes. CoAP is designed to easily translate to HTTP for simplified integration with the web, while also meeting specialized requirements such as multicast support, very low overhead, and simplicity. [1] [2] Multicast, low overhead, and simplicity are important for Internet of things (IoT) and machine-to-machine (M2M) communication, which tend to be embedded and have much less memory and power supply than traditional Internet devices have. Therefore, efficiency is very important. CoAP can run on most devices that support UDP or a UDP analogue.

The Internet Engineering Task Force (IETF) Constrained RESTful Environments Working Group (CoRE) has done the major standardization work for this protocol. In order to make the protocol suitable to IoT and M2M applications, various new functions have been added.

Specification

The core of the protocol is specified in RFC   7252. Various extensions have been proposed, particularly:

Message formats

CoAP makes use of two message types, requests and responses, using a simple, binary header format. CoAP is by default bound to UDP and optionally to DTLS, providing a high level of communications security. When bound to UDP, the entire message must fit within a single datagram. When used with 6LoWPAN as defined in RFC 4944, messages should fit into a single IEEE 802.15.4 frame to minimize fragmentation.

The smallest CoAP message is 4 bytes in length, if the token, options and payload fields are omitted, i.e. if it only consists of the CoAP header. The header is followed by the token value (0 to 8 bytes) which may be followed by a list of options in an optimized type–length–value format. Any bytes after the header, token and options (if any) are considered the message payload, which is prefixed by the one-byte "payload marker" (0xFF). The length of the payload is implied by the datagram length.

CoAP Message
Octet offset0123
Bit offset012345678910111213141516171819202122232425262728293031
432vertypetoken lengthrequest/response codemessage ID
864token (0–8 bytes)
1296
16128options (if available)
2016011111111payload (if available)

CoAP Fixed-Size Header

The first 4 bytes are mandatory in all CoAP datagrams, they constitute the fixed-size header.

These fields can be extracted from these 4 bytes in C via these macros:

#define COAP_HEADER_VERSION(data)  ( (0xC0 & (data)[0]) >> 6      )#define COAP_HEADER_TYPE(data)     ( (0x30 & (data)[0]) >> 4      )#define COAP_HEADER_TKL(data)      ( (0x0F & (data)[0]) >> 0      )#define COAP_HEADER_CLASS(data)    ( ((data)[1] >> 5) & 0x07      )#define COAP_HEADER_CODE(data)     ( ((data)[1] >> 0) & 0x1F      )#define COAP_HEADER_MID(data)      ( ((data)[2] << 8) | (data)[3] )

Version (ver) (2 bits)

Indicates the CoAP version number.

Type (2 bits)

This describes the datagram's message type for the two message type context of Request and Response.
  • Request
    • 0 : Confirmable : This message expects a corresponding acknowledgement message.
    • 1 : Non-confirmable : This message does not expect a confirmation message.
  • Response
    • 2 : Acknowledgement : This message is a response that acknowledge a confirmable message
    • 3 : Reset : This message indicates that it had received a message but could not process it.

Token length (4 bits)

Indicates the length of the variable-length Token field, which may be 0–8 bytes in length.

Request/response code (8 bits)

01234567
ClassCode

The three most significant bits form a number known as the "class", which is analogous to the class of HTTP status codes. The five least significant bits form a code that communicates further detail about the request or response. The entire code is typically communicated in the form class.code .

You can find the latest CoAP request/response codes at , though the below list gives some examples:

Message ID (16 bits)

Used to detect message duplication and to match messages of type acknowledgement/reset to messages of type confirmable/non-confirmable.

Token

Every request carries a token (but it may be zero length) whose value was generated by the client. The server must echo every token value without any modification back to the client in the corresponding response. It is intended for use as a client-local identifier to match requests and responses, especially for concurrent requests.

Matching requests and responses is not done with the message ID because a response may be sent in a different message than the acknowledgement (which uses the message ID for matching). For example, this could be done to prevent retransmissions if obtaining the result takes some time. Such a detached response is called "separate response". In contrast, transmitting the response directly in the acknowledgement is called "piggybacked response" which is expected to be preferred for efficiency reasons.

Option

Option Format
Bit position
01234567
Option deltaOption length
Option delta extended (none, 8 bits, 16 bits)
Option length extended (none, 8 bits, 16 bits)
Option value

Option delta:

Option length:

Option value:

Implementations

NameProgramming LanguageImplemented CoAP versionClient/ServerImplemented CoAP featuresLicenseLink
coapDartRFC 7252ClientBlockwise Transfers, Observe, Multicast, Proxying (partial)MIT https://github.com/shamblett/coap
aiocoapPython 3RFC 7252, RFC 7641, RFC 7959, RFC 8323, RFC 7967, RFC 8132, RFC 9176, RFC 8613, RFC 9528Client + ServerBlockwise Transfers, Observe (partial)MIT https://pypi.python.org/pypi/aiocoap
CaliforniumJavaRFC 7252, RFC 7641, RFC 7959Client + ServerObserve, Blockwise Transfers, Multicast (since 2.x), DTLS (+ DTLS 1.2 Connection ID)EPL+EDL https://www.eclipse.org/californium https://github.com/eclipse/californium
cantcoapC++/CRFC 7252Client + ServerBSD https://github.com/staropram/cantcoap
Canopus Go RFC 7252Client + ServerCoreApache License 2.0 https://github.com/zubairhamed/canopus
Go-CoAP Go RFC 7252, RFC 8232, RFC 7641, RFC 7959Client + ServerCore, Observe, Blockwise, Multicast, TCP/TLSApache License 2.0 https://github.com/plgd-dev/go-coap
CoAP implementation for Go Go RFC 7252Client + ServerCore + Draft SubscribeMIT https://github.com/dustin/go-coap
CoAP.NETC#RFC 7252, coap-13, coap-08, coap-03Client + ServerCore, Observe, Blockwise Transfers3-clause BSD https://github.com/smeshlink/CoAP.NET
CoAPSharpC#, .NETRFC 7252Client + ServerCore, Observe, Block, RDLGPL http://www.coapsharp.com
CoAPthonPythonRFC 7252Client + Server + Forward Proxy + Reverse ProxyObserve, Multicast server discovery, CoRE Link Format parsing, Block-wiseMIT https://github.com/Tanganelli/CoAPthon
CoAP ShellJavaRFC 7252ClientObserve, Blockwise Transfers, DTLSApache License 2.0 https://github.com/tzolov/coap-shell
CopperJavaScript (Browser Plugin)RFC 7252ClientObserve, Blockwise Transfers3-clause BSD https://github.com/mkovatsc/Copper https://addons.mozilla.org/firefox/addon/copper-270430/%5B‍%5D
eCoAPCRFC 7252Client + ServerCoreMIT https://gitlab.com/jobol/ecoap
Erbium for ContikiCRFC 7252Client + ServerObserve, Blockwise Transfers3-clause BSD http://www.contiki-os.org/ (er-rest-example)
FreeCoAPCRFC 7252Client + Server + HTTP/CoAP ProxyCore, DTLS, Blockwise TransfersBSD https://github.com/keith-cullen/FreeCoAP
guile-coapGuileRFC 7252, RFC 8323Client + ServerGPL-3.0-or-later https://codeberg.org/eris/guile-coap
iCoAPObjective-CRFC 7252ClientCore, Observe, Blockwise TransfersMIT https://github.com/stuffrabbit/iCoAP
java-coapJavaRFC 7252, RFC 7641, RFC 7959, RFC 8323Client + ServerApache License 2.0 https://github.com/PelionIoT/java-coap
jCoAPJavaRFC 7252Client + ServerObserve, Blockwise TransfersApache License 2.0 https://code.google.com/p/jcoap/
libcoapCRFC 7252, RFC 7390, RFC 7641, RFC 7959, RFC 7967, RFC 8132, RFC 8323, RFC 8516, RFC 8613, RFC 8768, RFC 8974, RFC 9175, RFC 9177Client + ServerCore, Observe, Multicast, Blockwise Transfers, Patch/Fetch, OSCORE, DTLSBSD/GPL https://github.com/obgm/libcoap
LibNyociCRFC 7252Client + ServerCore, Observe, Block, DTLSMIT https://github.com/darconeous/libnyoci
lobaro-coapCRFC 7252Client + ServerObserve, Blockwise TransfersMIT http://www.lobaro.com/lobaro-coap
microcoapCRFC 7252Client + ServerMIT https://github.com/1248/microcoap
microCoAPyMicroPythonRFC 7252Client + ServerCoreApache License 2.0 https://github.com/insighio/microCoAPy
nanoCoAPCRFC 7252Client + ServerCore, Blockwise Transfers, DTLSLGPL https://api.riot-os.org/group__net__nanocoap.html
nCoapJavaRFC 7252Client + ServerObserve, Blockwise Transfers, CoRE Link Format, Endpoint-ID-Draft BSD https://github.com/okleine/nCoAP
node-coapJavascriptRFC 7252,

RFC 7641, RFC 7959

Client + ServerCore, Observe, BlockMIT https://github.com/mcollina/node-coap
Qt CoAPC++RFC 7252ClientCore, Observe, Blockwise TransfersGPL, Commercial https://doc.qt.io/qt-6/qtcoap-index.html
Ruby coapRubyRFC 7252Client + Server (david)Core, Observe, Block, RDMIT, GPL https://github.com/nning/coap
https://github.com/nning/david
Sensinode C Device LibraryCRFC 7252Client + ServerCore, Observe, Block, RDCommercial https://silver.arm.com/browse/SEN00
Sensinode Java Device LibraryJava SERFC 7252Client + ServerCore, Observe, Block, RDCommercial https://silver.arm.com/browse/SEN00
Sensinode NanoService PlatformJava SERFC 7252Cloud ServerCore, Observe, Block, RDCommercial https://silver.arm.com/browse/SEN00
SwiftCoAPSwiftRFC 7252Client + ServerCore, Observe, Blockwise TransfersMIT https://github.com/stuffrabbit/SwiftCoAP
TinyOS CoapBlipnesC/Ccoap-13Client + ServerObserve, Blockwise TransfersBSD https://web.archive.org/web/20130312140509/http://docs.tinyos.net/tinywiki/index.php/CoAP
txThingsPython (Twisted)RFC 7252Client + ServerBlockwise Transfers, Observe (partial)MIT https://github.com/mwasilak/txThings/
coap-rsRustRFC 7252Client + ServerCore, Multicast, Observe option, Too Many Requests Response CodeMIT https://github.com/Covertness/coap-rs

https://docs.rs/coap/

YaCoAPCMIT https://github.com/RIOT-Makers/YaCoAP

Proxy implementations

CoAP group communication

In many CoAP application domains it is essential to have the ability to address several CoAP resources as a group, instead of addressing each resource individually (e.g. to turn on all the CoAP-enabled lights in a room with a single CoAP request triggered by toggling the light switch). To address this need, the IETF has developed an optional extension for CoAP in the form of an experimental RFC: Group Communication for CoAP - RFC 7390 [3] This extension relies on IP multicast to deliver the CoAP request to all group members. The use of multicast has certain benefits such as reducing the number of packets needed to deliver the request to the members. However, multicast also has its limitations such as poor reliability and being cache-unfriendly. An alternative method for CoAP group communication that uses unicasts instead of multicasts relies on having an intermediary where the groups are created. Clients send their group requests to the intermediary, which in turn sends individual unicast requests to the group members, collects the replies from them, and sends back an aggregated reply to the client. [4]

Security

CoAP defines four security modes: [5]

Research has been conducted on optimizing DTLS by implementing security associates as CoAP resources rather than using DTLS as a security wrapper for CoAP traffic. This research has indicated that improvements of up to 6.5 times none optimized implementations. [6]

In addition to DTLS, RFC8613 [7] defines the Object Security for Constrained RESTful Environments (OSCORE) protocol which provides security for CoAP at the application layer.

Security issues

Although the protocol standard includes provisions for mitigating the threat of DDoS amplification attacks, [8] these provisions are not implemented in practice, [9] resulting in the presence of over 580,000 targets primarily located in China and attacks up to 320 Gbit/s. [10]

See also

References