-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathoid_info.c
More file actions
129 lines (114 loc) · 2.79 KB
/
oid_info.c
File metadata and controls
129 lines (114 loc) · 2.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/*
* Part of `snmp-query-engine`.
*
* Copyright 2012-2013, Anton Berezin <tobez@tobez.org>
* Modified BSD license.
* (See LICENSE file in the distribution.)
*
*/
#include "sqe.h"
int
free_oid_info_list(struct oid_info_head *list)
{
struct oid_info *n1, *n2;
n1 = TAILQ_FIRST(list);
while (n1 != NULL) {
n2 = TAILQ_NEXT(n1, oid_list);
if (0){
fprintf(stderr, " freeing an oid (C:%u,S:%u) %s\n", n1->cid, n1->sid, oid2str(n1->oid));
}
free(n1->oid.buf);
free(n1->value.buf);
PS.active_oid_infos--;
free(n1);
n1 = n2;
}
TAILQ_INIT(list);
return 1;
}
int
free_oid_info(struct oid_info *oi)
{
free(oi->oid.buf);
free(oi->value.buf);
PS.active_oid_infos--;
free(oi);
return 1;
}
int
allocate_oid_info_list(struct oid_info_head *list, msgpack_object *o, struct cid_info *ci)
{
int i;
struct oid_info *oi;
char tmp_buf[2048];
struct ber e;
for (i = 0; i < o->via.array.size; i++) {
if (o->via.array.ptr[i].type == MSGPACK_OBJECT_BIN) {
e = ber_init(tmp_buf, 2048);
if (encode_string_oid(o->via.array.ptr[i].via.bin.ptr, o->via.array.ptr[i].via.bin.size, &e) < 0) goto not_good;
} else if (o->via.array.ptr[i].type == MSGPACK_OBJECT_STR) {
e = ber_init(tmp_buf, 2048);
if (encode_string_oid(o->via.array.ptr[i].via.str.ptr, o->via.array.ptr[i].via.str.size, &e) < 0) goto not_good;
} else {
goto not_good;
}
oi = malloc(sizeof(*oi));
if (!oi)
croak(2, "allocate_oid_info_list: malloc(oid_info)");
PS.active_oid_infos++;
PS.total_oid_infos++;
bzero(oi, sizeof(*oi));
oi->cid = ci->cid;
oi->fd = ci->fd;
oi->oid = ber_dup(&e);
TAILQ_INSERT_TAIL(list, oi, oid_list);
}
return o->via.array.size;
not_good:
free_oid_info_list(list);
return 0;
}
struct oid_info *
allocate_oid_info(msgpack_object *o, struct cid_info *ci)
{
struct oid_info *oi;
char tmp_buf[2048];
struct ber e;
if (o->type == MSGPACK_OBJECT_BIN) {
e = ber_init(tmp_buf, 2048);
if (encode_string_oid(o->via.bin.ptr, o->via.bin.size, &e) < 0) return NULL;
} else if (o->type == MSGPACK_OBJECT_STR) {
e = ber_init(tmp_buf, 2048);
if (encode_string_oid(o->via.str.ptr, o->via.str.size, &e) < 0) return NULL;
} else {
return NULL;
}
oi = malloc(sizeof(*oi));
if (!oi)
croak(2, "allocate_oid_info: malloc(oid_info)");
PS.active_oid_infos++;
PS.total_oid_infos++;
bzero(oi, sizeof(*oi));
oi->cid = ci->cid;
oi->fd = ci->fd;
oi->oid = ber_dup(&e);
return oi;
}
void
dump_oid_info(msgpack_packer *pk, struct oid_info *oi)
{
#define DUMPi(field) msgpack_pack_named_int(pk, #field, oi->field)
msgpack_pack_map(pk, 6);
DUMPi(sid);
DUMPi(cid);
DUMPi(fd);
DUMPi(max_repetitions);
msgpack_pack_string(pk, "oid");
msgpack_pack_oid(pk, oi->oid);
msgpack_pack_string(pk, "value");
if (!oi->value.buf)
msgpack_pack_nil(pk);
else
msgpack_pack_ber(pk, oi->value);
#undef DUMPi
}