2 * Copyright (C) 2008 Stig Venaas <venaas@uninett.no>
3 * Copyright (C) 2010 NORDUnet A/S
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
11 #include <sys/inttypes.h>
19 #include <arpa/inet.h>
21 struct tlv *maketlv(uint8_t t, uint8_t l, void *v) {
24 tlv = malloc(sizeof(struct tlv));
41 struct tlv *copytlv(struct tlv *in) {
42 return in ? maketlv(in->t, in->l, in->v) : NULL;
45 void freetlv(struct tlv *tlv) {
52 int eqtlv(struct tlv *t1, struct tlv *t2) {
55 if (t1->t != t2->t || t1->l != t2->l)
57 return memcmp(t1->v, t2->v, t1->l) == 0;
60 struct list *copytlvlist(struct list *tlvs) {
62 struct list_node *node;
69 for (node = list_first(tlvs); node; node = list_next(node)) {
70 if (!list_push(out, copytlv((struct tlv *)node->data))) {
78 void freetlvlist(struct list *tlvs) {
80 while ((tlv = (struct tlv *)list_shift(tlvs)))
85 void rmtlv(struct list *tlvs, uint8_t t) {
86 struct list_node *n, *p;
92 tlv = (struct tlv *)n->data;
94 list_removedata(tlvs, tlv);
96 n = p ? list_next(p) : list_first(tlvs);
104 uint8_t *tlv2str(struct tlv *tlv) {
105 uint8_t *s = malloc(tlv->l + 1);
107 memcpy(s, tlv->v, tlv->l);
113 uint8_t *tlv2buf(uint8_t *p, const struct tlv *tlv) {
118 memcpy(p, tlv->v, tlv->l);
120 memset(p, 0, tlv->l);
125 /* Local Variables: */
126 /* c-file-style: "stroustrup" */