How to work with hexadecimal data types in YANG and ConfD

The hex-string data type as defined in RFC 6991 (Common YANG Data Types) can be used for hexadecimal data elements in your YANG data model. It is defined as:

 typedef hex-string {
   type string {
     pattern '([0-9a-fA-F]{2}(:[0-9a-fA-F]{2})*)?';
    "A hexadecimal string with octets represented as hex digits
     separated by colons.  The canonical representation uses
     lowercase characters.";

An example YANG definition for the hex-string data elements is as follows:

import ietf-yang-types {
  prefix yt;

leaf HexString {
  type yt:hex-string;
  default 0f:5b;

Internal to ConfD, the hex-string data type is being stored as a C_HEXSTR data type. The value representation is the same as for C_BUF and C_BINARY. C_HEXSTR data received by the application from ConfD is always NUL terminated, but since the data may also contain NUL bytes, it is generally necessary to use the size given by the representation. The struct which is used is:

typedef struct confd_buf {
    unsigned int size;
    unsigned char *ptr;

When values of this type are received by the callback functions in confd_lib_dp(3), the ptr field is a pointer to libconfd private memory, and the data will not survive unless copied by the application.

To create and extract values of type C_HEXSTR we do:

confd_value_t myval, myval2;
unsigned char *hex;
int len;
hex = CONFD_GET_HEXSTR_PTR(&myval);
len = CONFD_GET_HEXSTR_SIZE(&myval);
CONFD_SET_HEXSTR(&myval2, bin, len);

You can learn more about ConfD internal data types in Volume 3 of the ConfD man-pages under the confd_types section.