Skip to content

How to add an application checksum to messages

Problem

You want to add an application level checksum, or some other integrity check such as a signature to all messages without changing the data you send.

Note

UDP includes 16 bit checksums on messages. By default, the checksum is checked by the network adapter hardware or operating system before delivery to the application - and if the checksum check fails, the message is dropped. Aeron does not perform an additional check of the UDP checksum.

Solution

Use the ReservedValueSupplier variants of Publication.offer. This allows a 64 bit long to be sent in the Aeron header along with your message. You can place whatever you need in this long - it could be two 32 bit ints for example. The data is always encoded with ByteOrder.LITTLE_ENDIAN format.

Discussion

A ReservedValueSupplier is a callback that Aeron calls as the last action before the length is set on each message fragment, which happens just before it is delivered to the Media Driver for sending via IPC/UDP.

The interface looks like this:

public interface ReservedValueSupplier
{
    long get(DirectBuffer termBuffer, int termOffset, int frameLength);
}

An implementation could look something like:

private void doSomething()
{
    ...
    final long result = publication.offer(requestBuffer, 0, length, Helper::crc);
    ...
}
...
private long crc(DirectBuffer directBuffer, int termOffset, int frameLength)
{
    // return computed CRC32C or whatever needed, using the 
    // buffer provided from offset to framelength
}

See Also

Back to top