-
Notifications
You must be signed in to change notification settings - Fork 4
/
client_requests_info.c
194 lines (168 loc) · 4.62 KB
/
client_requests_info.c
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
/*
* Part of `snmp-query-engine`.
*
* Copyright 2012-2013, Anton Berezin <[email protected]>
* Modified BSD license.
* (See LICENSE file in the distribution.)
*
*/
#include "sqe.h"
static JudyL dest_by_fd; // -> JudyL(by ip) -> JudyL(by port) -> destination
struct client_requests_info *
get_client_requests_info(struct in_addr *ip, unsigned port, struct socket_info *si)
{
struct destination *dest;
void **fd_slot, **ip_slot;
struct client_requests_info **cri_slot, **dest_cri_slot, *cri;
dest = get_destination(ip, port);
JLI(fd_slot, dest_by_fd, si->fd);
if (fd_slot == PJERR)
croak(2, "get_client_requests_info: JLI(fd) failed");
JLI(ip_slot, *fd_slot, ip->s_addr);
if (ip_slot == PJERR)
croak(2, "get_client_requests_info: JLI(ip) failed");
JLI(cri_slot, *ip_slot, port);
if (cri_slot == PJERR)
croak(2, "get_client_requests_info: JLI(port) failed");
if (!*cri_slot) {
cri = malloc(sizeof(*cri));
if (!cri)
croak(2, "get_client_requests_info: malloc(cri)");
PS.active_cr_infos++;
PS.total_cr_infos++;
si->PS.active_cr_infos++;
si->PS.total_cr_infos++;
bzero(cri, sizeof(*cri));
cri->version = 1; /* 2c is the default */
strcpy(cri->community, "public");
cri->timeout = DEFAULT_TIMEOUT;
cri->retries = DEFAULT_RETRIES;
cri->dest = dest;
cri->fd = si->fd;
cri->si = si;
TAILQ_INIT(&cri->oids_to_query);
TAILQ_INIT(&cri->sid_infos);
*cri_slot = cri;
}
JLI(dest_cri_slot, dest->client_requests_info, si->fd);
if (dest_cri_slot == PJERR)
croak(2, "get_client_requests_info: JLI(dest/fd) failed");
*dest_cri_slot = *cri_slot;
return *cri_slot;
}
int
free_all_client_request_info_for_fd(int fd)
{
void **fd_slot, **ip_slot;
struct client_requests_info **cri_slot;
Word_t ip, port;
Word_t rc;
JLG(fd_slot, dest_by_fd, fd);
if (fd_slot == PJERR)
croak(2, "free_all_client_request_info_for_fd: JLG(fd) failed");
if (!fd_slot)
return 1;
ip = 0;
JLF(ip_slot, *fd_slot, ip);
while (ip_slot) {
port = 0;
JLF(cri_slot, *ip_slot, port);
while (cri_slot) {
free_client_request_info(*cri_slot);
JLN(cri_slot, *ip_slot, port);
}
JLFA(rc, *ip_slot);
JLN(ip_slot, *fd_slot, ip);
}
JLFA(rc, *fd_slot);
JLD(rc, dest_by_fd, fd);
return 1;
}
int
free_client_request_info(struct client_requests_info *cri)
{
struct cid_info **ci_slot;
Word_t cid;
Word_t rc;
struct sid_info *si, *si_temp;
PS.active_cr_infos--;
cri->si->PS.active_cr_infos--;
si = TAILQ_FIRST(&cri->sid_infos);
while (si != NULL) {
si_temp = TAILQ_NEXT(si, sid_list);
free_sid_info(si);
cri->dest->packets_on_the_wire--;
if (cri->dest->packets_on_the_wire < 0)
cri->dest->packets_on_the_wire = 0;
PS.packets_on_the_wire--;
if (PS.packets_on_the_wire < 0)
PS.packets_on_the_wire = 0;
si = si_temp;
}
TAILQ_INIT(&cri->sid_infos);
free_oid_info_list(&cri->oids_to_query);
cid = 0;
JLF(ci_slot, cri->cid_info, cid);
while (ci_slot) {
free_cid_info(*ci_slot);
JLN(ci_slot, cri->cid_info, cid);
}
JLFA(rc, cri->cid_info);
JLD(rc, cri->dest->client_requests_info, cri->fd);
free(cri);
return 1;
}
void
dump_client_request_info(msgpack_packer *pk, struct client_requests_info *cri)
{
char buf[512];
Word_t n_cid, n_query_queue, n_sid, cid;
struct oid_info *oi;
struct sid_info *si;
struct cid_info **cid_slot;
#define PACK msgpack_pack_string(pk, buf)
#define DUMPi(field) msgpack_pack_named_int(pk, #field, cri->field)
#define DUMPs(field) msgpack_pack_named_string(pk, #field, cri->field)
snprintf(buf, 512, "CRI(%d)", cri->fd); PACK;
msgpack_pack_map(pk, 12);
msgpack_pack_string(pk, "dest");
snprintf(buf, 512, "DEST(%s:%d)", inet_ntoa(cri->dest->ip), cri->dest->port); PACK;
DUMPi(fd);
DUMPi(version);
DUMPs(community);
DUMPi(timeout);
DUMPi(retries);
JLC(n_cid, cri->cid_info, 0, -1);
msgpack_pack_named_int(pk, "#CID", n_cid);
n_query_queue = 0;
TAILQ_FOREACH(oi, &cri->oids_to_query, oid_list) {
n_query_queue++;
}
msgpack_pack_named_int(pk, "#QUERY_QUEUE", n_query_queue);
msgpack_pack_string(pk, "@QUERY_QUEUE");
msgpack_pack_array(pk, n_query_queue);
TAILQ_FOREACH(oi, &cri->oids_to_query, oid_list) {
dump_oid_info(pk, oi);
}
n_sid = 0;
TAILQ_FOREACH(si, &cri->sid_infos, sid_list) {
n_sid++;
}
msgpack_pack_named_int(pk, "#SID", n_sid);
msgpack_pack_string(pk, "@CID");
msgpack_pack_map(pk, n_cid);
cid = 0;
JLF(cid_slot, cri->cid_info, cid);
while (cid_slot) {
dump_cid_info(pk, *cid_slot);
JLN(cid_slot, cri->cid_info, cid);
}
msgpack_pack_string(pk, "@SID");
msgpack_pack_map(pk, n_sid);
TAILQ_FOREACH(si, &cri->sid_infos, sid_list) {
dump_sid_info(pk, si);
}
#undef DUMPi
#undef DUMPs
#undef PACK
}