Use existing temporary variable conn.
[radsecproxy.git] / tlv11.c
1 /*
2  * Copyright (C) 2008 Stig Venaas <venaas@uninett.no>
3  *
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.
7  */
8
9 #ifdef SYS_SOLARIS9
10 #include <sys/inttypes.h>
11 #else
12 #include <stdint.h>
13 #endif
14 #include "list.h"
15 #include "tlv11.h"
16 #include <stdlib.h>
17 #include <string.h>
18 #include <arpa/inet.h>
19
20 struct tlv *maketlv(uint8_t t, uint8_t l, void *v) {
21     struct tlv *tlv;
22
23     tlv = malloc(sizeof(struct tlv));
24     if (!tlv)
25         return NULL;
26     tlv->t = t;
27     tlv->l = l;
28     if (l && v) {
29         tlv->v = malloc(l);
30         if (!tlv->v) {
31             free(tlv);
32             return NULL;
33         }
34         memcpy(tlv->v, v, l);
35     } else
36         tlv->v = NULL;
37     return tlv;
38 }
39
40 struct tlv *copytlv(struct tlv *in) {
41     return in ? maketlv(in->t, in->l, in->v) : NULL;
42 }
43
44 void freetlv(struct tlv *tlv) {
45     if (tlv) {
46         free(tlv->v);
47         free(tlv);
48     }
49 }
50
51 int eqtlv(struct tlv *t1, struct tlv *t2) {
52     if (!t1 || !t2)
53         return t1 == t2;
54     if (t1->t != t2->t || t1->l != t2->l)
55         return 0;
56     return memcmp(t1->v, t2->v, t1->l) == 0;
57 }
58
59 struct list *copytlvlist(struct list *tlvs) {
60     struct list *out;
61     struct list_node *node;
62
63     if (!tlvs)
64         return NULL;
65     out = list_create();
66     if (!out)
67         return NULL;
68     for (node = list_first(tlvs); node; node = list_next(node)) {
69         if (!list_push(out, copytlv((struct tlv *)node->data))) {
70             freetlvlist(out);
71             return NULL;
72         }
73     }
74     return out;
75 }
76
77 void freetlvlist(struct list *tlvs) {
78     struct tlv *tlv;
79     while ((tlv = (struct tlv *)list_shift(tlvs)))
80         freetlv(tlv);
81     list_destroy(tlvs);
82 }
83
84 void rmtlv(struct list *tlvs, uint8_t t) {
85     struct list_node *n, *p;
86     struct tlv *tlv;
87
88     p = NULL;
89     n = list_first(tlvs);
90     while (n) {
91         tlv = (struct tlv *)n->data;
92         if (tlv->t == t) {
93             list_removedata(tlvs, tlv);
94             freetlv(tlv);
95             n = p ? list_next(p) : list_first(tlvs);
96         } else {
97             p = n;
98             n = list_next(n);
99         }
100     }
101 }
102
103 uint8_t *tlv2str(struct tlv *tlv) {
104     uint8_t *s = malloc(tlv->l + 1);
105     if (s) {
106         memcpy(s, tlv->v, tlv->l);
107         s[tlv->l] = '\0';
108     }
109     return s;
110 }
111
112 uint8_t *tlv2buf(uint8_t *p, const struct tlv *tlv) {
113     *p++ = tlv->t;
114     *p++ = tlv->l;
115     if (tlv->l) {
116         if (tlv->v)
117             memcpy(p, tlv->v, tlv->l);
118         else
119             memset(p, 0, tlv->l);
120     }
121     return p;
122 }
123
124 /* Local Variables: */
125 /* c-file-style: "stroustrup" */
126 /* End: */