DevEUI for non-hardware assigned values

You’ll have to judge whether you want to use a random ID. However, the definition of EUI-64 allows for this. The definition is given in chapter 8 of the IEEE-802 spec (freely available at http://standards.ieee.org/getieee802/download/802-2014.pdf).

In section 8.2, they describe the u/l bit (bit 1) of byte 0 of EUI-64. If you set that bit, you have a “locally administered address”. Bit 0 always has to be zero for addresses. So a legitimate way of generating a random address, with very low probability of collision, is to generate 8 random bytes, then set bit 1 of byte 0, clear bit 0 of byte 0. On Ubuntu, this simple program will do what you need.

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/fcntl.h>
int main(int ac, char **av) {
    int fd, i;
    unsigned char eui[8];
    fd = open("/dev/random", O_RDONLY);
    if (fd < 0) {
        perror("can't open /dev/random");
        exit(1);
    }
    if (read(fd, eui, sizeof(eui)) != sizeof(eui)) {
        fprintf(stderr, "couldn't read %zu bytes\n", sizeof(eui));
        exit(1);
    }
    eui[0] = (eui[0] & ~1) | 2;
    for (i = 0; i < sizeof(eui); ++i) {
        printf("%02X%c", eui[i], i == sizeof(eui)-1 ? '\n' : '-');
    }
    return 0;
}
8 Likes