2 * Copyright (C) 2008 Stig Venaas <venaas@uninett.no>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
10 #include <sys/inttypes.h>
18 #include <arpa/inet.h>
20 struct tlv *maketlv(uint8_t t, uint8_t l, void *v) {
23 tlv = malloc(sizeof(struct tlv));
40 struct tlv *copytlv(struct tlv *in) {
41 return in ? maketlv(in->t, in->l, in->v) : NULL;
44 void freetlv(struct tlv *tlv) {
51 int eqtlv(struct tlv *t1, struct tlv *t2) {
54 if (t1->t != t2->t || t1->l != t2->l)
56 return memcmp(t1->v, t2->v, t1->l) == 0;
59 struct list *copytlvlist(struct list *tlvs) {
61 struct list_node *node;
68 for (node = list_first(tlvs); node; node = list_next(node)) {
69 if (!list_push(out, copytlv((struct tlv *)node->data))) {
77 void freetlvlist(struct list *tlvs) {
79 while ((tlv = (struct tlv *)list_shift(tlvs)))
84 void rmtlv(struct list *tlvs, uint8_t t) {
85 struct list_node *n, *p;
91 tlv = (struct tlv *)n->data;
93 list_removedata(tlvs, tlv);
95 n = p ? list_next(p) : list_first(tlvs);
103 uint8_t *tlv2str(struct tlv *tlv) {
104 uint8_t *s = malloc(tlv->l + 1);
106 memcpy(s, tlv->v, tlv->l);
112 uint8_t *tlv2buf(uint8_t *p, const struct tlv *tlv) {
117 memcpy(p, tlv->v, tlv->l);
119 memset(p, 0, tlv->l);
124 /* Local Variables: */
125 /* c-file-style: "stroustrup" */