MINOR: sample: manage binary to string type convertion in stick-table and samples.
Binary type is converted to a null terminated hexa string.
diff --git a/src/sample.c b/src/sample.c
index 3eef4c3..b9e3c18 100644
--- a/src/sample.c
+++ b/src/sample.c
@@ -187,6 +187,22 @@
return inet_pton(AF_INET6, smp->data.str.str, &smp->data.ipv6);
}
+static int c_bin2str(struct sample *smp)
+{
+ struct chunk *trash = get_trash_chunk();
+ unsigned char c;
+ int ptr = 0;
+
+ trash->len = 0;
+ while (ptr < smp->data.str.len && trash->len <= trash->size - 2) {
+ c = smp->data.str.str[ptr++];
+ trash->str[trash->len++] = hextab[(c >> 4) & 0xF];
+ trash->str[trash->len++] = hextab[c & 0xF];
+ }
+ smp->data.str = *trash;
+ return 1;
+}
+
static int c_int2str(struct sample *smp)
{
struct chunk *trash = get_trash_chunk();
@@ -253,9 +269,9 @@
/* IPV4 */ { NULL, c_ip2int, c_ip2int, c_none, c_ip2ipv6, c_ip2str, NULL, c_ip2str, NULL },
/* IPV6 */ { NULL, NULL, NULL, NULL, c_none, c_ipv62str, NULL, c_ipv62str, NULL },
/* STR */ { c_str2int, c_str2int, c_str2int, c_str2ip, c_str2ipv6, c_none, c_none, c_none, c_none },
-/* BIN */ { NULL, NULL, NULL, NULL, NULL, NULL, c_none, NULL, c_none },
+/* BIN */ { NULL, NULL, NULL, NULL, NULL, c_bin2str, c_none, c_bin2str, c_none },
/* CSTR */ { c_str2int, c_str2int, c_str2int, c_str2ip, c_str2ipv6, c_datadup, c_datadup, c_none, c_none },
-/* CBIN */ { NULL, NULL, NULL, NULL, NULL, NULL, c_datadup, NULL, c_none },
+/* CBIN */ { NULL, NULL, NULL, NULL, NULL, c_bin2str, c_datadup, c_bin2str, c_none },
};
/*
diff --git a/src/stick_table.c b/src/stick_table.c
index 52ea7cf..75267e1 100644
--- a/src/stick_table.c
+++ b/src/stick_table.c
@@ -504,6 +504,21 @@
return (void *)kdata->buf;
}
+static void *k_bin2str(struct sample *smp, union stktable_key_data *kdata, size_t *len)
+{
+ unsigned char c;
+ int ptr = 0;
+
+ *len = 0;
+ while (ptr < smp->data.str.len && *len <= sizeof(kdata->buf) - 2) {
+ c = smp->data.str.str[ptr++];
+ kdata->buf[(*len)++] = hextab[(c >> 4) & 0xF];
+ kdata->buf[(*len)++] = hextab[c & 0xF];
+ }
+
+ return (void *)kdata->buf;
+}
+
static void *k_ipv62str(struct sample *smp, union stktable_key_data *kdata, size_t *len)
{
if (!inet_ntop(AF_INET6, &smp->data.ipv6, kdata->buf, sizeof(kdata->buf)))
@@ -578,9 +593,9 @@
/* IPV4 */ { k_ip2ip, k_ip2ipv6, k_ip2int, k_ip2str, NULL },
/* IPV6 */ { NULL, k_ipv62ipv6, NULL, k_ipv62str, NULL },
/* STR */ { k_str2ip, k_str2ipv6, k_str2int, k_str2str, k_str2str },
-/* BIN */ { NULL, NULL, NULL, NULL, k_str2str },
+/* BIN */ { NULL, NULL, NULL, k_bin2str, k_str2str },
/* CSTR */ { k_str2ip, k_str2ipv6, k_str2int, k_str2str, k_str2str },
-/* CBIN */ { NULL, NULL, NULL, NULL , k_str2str },
+/* CBIN */ { NULL, NULL, NULL, k_bin2str, k_str2str },
};