summaryrefslogtreecommitdiffstats
path: root/lib/xref.c
diff options
context:
space:
mode:
authorDavid Lamparter <equinox@opensourcerouting.org>2018-08-09 22:50:19 +0200
committerDavid Lamparter <equinox@diac24.net>2021-02-01 17:18:02 +0100
commit8e427c293860576df43ead3639bac807e2a43b03 (patch)
tree88a02353048fce9fb350d7a1231322fb5830d5c7 /lib/xref.c
parentlib: move frr_weak_random to header file (diff)
downloadfrr-8e427c293860576df43ead3639bac807e2a43b03.tar.xz
frr-8e427c293860576df43ead3639bac807e2a43b03.zip
lib: "xref" identifier infrastructure
This adds the machinery for cross reference points (hence "xref") for things to be annotated with source code location or other metadata and/or to be uniquely identified and found at runtime or by dissecting executable files. The extraction tool to walk down an ELF file is done and working but needs some more cleanup and will be added in a separate commit. Signed-off-by: David Lamparter <equinox@diac24.net>
Diffstat (limited to 'lib/xref.c')
-rw-r--r--lib/xref.c122
1 files changed, 122 insertions, 0 deletions
diff --git a/lib/xref.c b/lib/xref.c
new file mode 100644
index 000000000..eb5e8ed2c
--- /dev/null
+++ b/lib/xref.c
@@ -0,0 +1,122 @@
+/*
+ * Copyright (c) 2017-20 David Lamparter, for NetDEF, Inc.
+ *
+ * Permission to use, copy, modify, and distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+
+#include <stdlib.h>
+#include <stdarg.h>
+#include <string.h>
+#include <pthread.h>
+#include <signal.h>
+#include <inttypes.h>
+
+#include "xref.h"
+#include "vty.h"
+#include "jhash.h"
+#include "sha256.h"
+#include "memory.h"
+#include "hash.h"
+
+struct xref_block *xref_blocks;
+static struct xref_block **xref_block_last = &xref_blocks;
+
+static void base32(uint8_t **inpos, int *bitpos,
+ char *out, size_t n_chars)
+{
+ static const char base32ch[] = "0123456789ABCDEFGHJKMNPQRSTVWXYZ";
+
+ char *opos = out;
+ uint8_t *in = *inpos;
+ int bp = *bitpos;
+
+ while (opos < out + n_chars) {
+ uint32_t bits = in[0] | (in[1] << 8);
+
+ if (bp == -1)
+ bits |= 0x10;
+ else
+ bits >>= bp;
+
+ *opos++ = base32ch[bits & 0x1f];
+
+ bp += 5;
+ if (bp >= 8)
+ in++, bp -= 8;
+ }
+ *opos = '\0';
+ *inpos = in;
+ *bitpos = bp;
+}
+
+void xref_block_add(struct xref_block *block)
+{
+ const struct xref * const *xrefp;
+ SHA256_CTX sha;
+
+ *xref_block_last = block;
+ xref_block_last = &block->next;
+
+ for (xrefp = block->start; xrefp < block->stop; xrefp++) {
+ const struct xref *xref = *xrefp;
+ struct xrefdata *xrefdata;
+
+ const char *filename, *p, *q;
+ uint8_t hash[32], *h = hash;
+ uint32_t be_val;
+ int bitpos;
+
+ if (!xref || !xref->xrefdata)
+ continue;
+
+ xrefdata = xref->xrefdata;
+ xrefdata->xref = xref;
+
+ if (!xrefdata->hashstr)
+ continue;
+
+ /* as far as the unique ID is concerned, only use the last
+ * directory name + filename, e.g. "bgpd/bgp_route.c". This
+ * gives a little leeway in moving things and avoids IDs being
+ * screwed up by out of tree builds or absolute pathnames.
+ */
+ filename = xref->file;
+ p = strrchr(filename, '/');
+ if (p) {
+ q = memrchr(filename, '/', p - filename);
+ if (q)
+ filename = q + 1;
+ else
+ filename = p + 1;
+ }
+
+ SHA256_Init(&sha);
+ SHA256_Update(&sha, filename, strlen(filename));
+ SHA256_Update(&sha, xrefdata->hashstr,
+ strlen(xrefdata->hashstr));
+ be_val = htonl(xrefdata->hashu32[0]);
+ SHA256_Update(&sha, &be_val, sizeof(be_val));
+ be_val = htonl(xrefdata->hashu32[1]);
+ SHA256_Update(&sha, &be_val, sizeof(be_val));
+ SHA256_Final(hash, &sha);
+
+ bitpos = -1;
+ base32(&h, &bitpos, &xrefdata->uid[0], 5);
+ xrefdata->uid[5] = '-';
+ base32(&h, &bitpos, &xrefdata->uid[6], 5);
+ }
+}