Debian Codegraph @

perl/OID/OID.xs

raw
1 /* -*- C -*- */
2
3 #pragma GCC diagnostic push
4 #pragma GCC diagnostic ignored "-Wcast-qual"
5 #pragma GCC diagnostic ignored "-Wdeclaration-after-statement"
6 #include "EXTERN.h"
7 #include "perl.h"
8 #include "XSUB.h"
9 #pragma GCC diagnostic pop
10
11 #include <net-snmp/net-snmp-config.h>
12 #include <net-snmp/net-snmp-includes.h>
13
14 /* pulled from Dave's, yet-to-be-used, net-snmp library rewrite.
15 autocompatibility for the future? */
16
17 typedef struct netsnmp_oid_s {
18 oid *name;
19 size_t len;
20 oid namebuf[ MAX_OID_LEN ];
21 } netsnmp_oid;
22
23 static int constant(double *value, const char *name, const int len)
24 {
25 return EINVAL;
26 }
27
28 netsnmp_oid *
29 nso_newarrayptr(oid *name, size_t name_len)
30 {
31 netsnmp_oid *RETVAL;
32 RETVAL = malloc(sizeof(netsnmp_oid));
33 RETVAL->name = RETVAL->namebuf;
34 RETVAL->len = name_len;
35 memcpy(RETVAL->name, name, name_len * sizeof(oid));
36 return RETVAL;
37 }
38
39 static int __snprint_num_objid _((char *, size_t, const oid *, int));
40
41 /* stolen from SNMP.xs. Ug, this needs merging to snmplib */
42 /* XXX: this is only here because snmplib forces quotes around the
43 data and won't return real binary data or a numeric string. Every
44 app must do its own switch() to get around it. Ug. */
45 #define USE_BASIC 0
46 #define USE_ENUMS 1
47 #define USE_SPRINT_VALUE 2
48 static int
49 __snprint_value(char *buf, size_t buf_len, netsnmp_variable_list *var,
50 struct tree *tp, int type, int flag)
51 {
52 int len = 0;
53 u_char* ip;
54 struct enum_list *ep;
55
56
57 buf[0] = '\0';
58 if (flag == USE_SPRINT_VALUE) {
59 snprint_value(buf, buf_len, var->name, var->name_length, var);
60 len = strlen(buf);
61 } else {
62 switch (var->type) {
63 case ASN_INTEGER:
64 if (flag == USE_ENUMS) {
65 for(ep = tp->enums; ep; ep = ep->next) {
66 if (ep->value == *var->val.integer) {
67 strcpy(buf, ep->label);
68 len = strlen(buf);
69 break;
70 }
71 }
72 }
73 if (!len) {
74 sprintf(buf,"%ld", *var->val.integer);
75 len = strlen(buf);
76 }
77 break;
78
79 case ASN_GAUGE:
80 case ASN_COUNTER:
81 case ASN_TIMETICKS:
82 case ASN_UINTEGER:
83 sprintf(buf,"%lu", (unsigned long) *var->val.integer);
84 len = strlen(buf);
85 break;
86
87 case ASN_OCTET_STR:
88 case ASN_OPAQUE:
89 memcpy(buf, (char*)var->val.string, var->val_len);
90 len = var->val_len;
91 break;
92
93 case ASN_IPADDRESS:
94 ip = (u_char*)var->val.string;
95 sprintf(buf, "%d.%d.%d.%d", ip[0], ip[1], ip[2], ip[3]);
96 len = strlen(buf);
97 break;
98
99 case ASN_NULL:
100 break;
101
102 case ASN_OBJECT_ID:
103 __snprint_num_objid(buf, buf_len, var->val.objid,
104 var->val_len / sizeof(oid));
105 len = strlen(buf);
106 break;
107
108 case SNMP_ENDOFMIBVIEW:
109 sprintf(buf,"%s", "ENDOFMIBVIEW");
110 break;
111 case SNMP_NOSUCHOBJECT:
112 sprintf(buf,"%s", "NOSUCHOBJECT");
113 break;
114 case SNMP_NOSUCHINSTANCE:
115 sprintf(buf,"%s", "NOSUCHINSTANCE");
116 break;
117
118 case ASN_COUNTER64:
119 printU64(buf,(struct counter64 *)var->val.counter64);
120 len = strlen(buf);
121 break;
122
123 case ASN_BIT_STR:
124 snprint_bitstring(buf, buf_len, var, NULL, NULL, NULL);
125 len = strlen(buf);
126 break;
127
128 case ASN_NSAP:
129 default:
130 warn("snprint_value: asn type not handled %d\n",var->type);
131 }
132 }
133 return(len);
134 }
135
136 static int
137 __snprint_num_objid (buf, buf_len, objid, len)
138 char *buf;
139 size_t buf_len;
140 const oid *objid;
141 int len;
142 {
143 const char *const end = buf + buf_len;
144 int i;
145
146 buf[0] = '\0';
147 for (i=0; i < len; i++) {
148 snprintf(buf, end - buf, ".%" NETSNMP_PRIo "u", *objid++);
149 buf += strlen(buf);
150 }
151 return SNMPERR_SUCCESS;
152 }
153
154 MODULE = NetSNMP::OID PACKAGE = NetSNMP::OID PREFIX=nso_
155
156 netsnmp_oid *
157 nso_newptr(initstring)
158 char *initstring
159 CODE:
160 if (get_tree_head() == NULL)
161 netsnmp_init_mib();
162 RETVAL = malloc(sizeof(netsnmp_oid));
163 RETVAL->name = RETVAL->namebuf;
164 RETVAL->len = sizeof(RETVAL->namebuf)/sizeof(RETVAL->namebuf[0]);
165 if (!snmp_parse_oid(initstring, (oid *) RETVAL->name, &RETVAL->len)) {
166 snmp_log(LOG_ERR, "Can't parse: %s\n", initstring);
167 RETVAL->len = 0;
168 free(RETVAL);
169 RETVAL = NULL;
170 }
171 OUTPUT:
172 RETVAL
173
174 void
175 constant(sv)
176 PREINIT:
177 STRLEN len;
178 INPUT:
179 SV * sv
180 char * s = SvPV(sv, len);
181 INIT:
182 int status;
183 double value;
184 PPCODE:
185 value = 0;
186 status = constant(&value, s, len);
187 XPUSHs(sv_2mortal(newSVuv(status)));
188 XPUSHs(sv_2mortal(newSVnv(value)));
189
190 int
191 _snmp_oid_compare(oid1, oid2)
192 netsnmp_oid *oid1;
193 netsnmp_oid *oid2;
194 CODE:
195 RETVAL = snmp_oid_compare((oid *) oid1->name, oid1->len,
196 (oid *) oid2->name, oid2->len);
197 OUTPUT:
198 RETVAL
199
200 MODULE = NetSNMP::OID PACKAGE = netsnmp_oidPtr PREFIX = nsop_
201
202 void
203 nsop_DESTROY(oid1)
204 netsnmp_oid *oid1
205 CODE:
206 {
207 if (oid1->name != oid1->namebuf) {
208 free(oid1->name);
209 }
210 free(oid1);
211 }
212
213 char *
214 nsop_to_string(oid1)
215 netsnmp_oid *oid1
216 PREINIT:
217 static char mystr[SNMP_MAXBUF];
218 CODE:
219 {
220 if (oid1->len == 0)
221 snprintf(mystr, sizeof(mystr), "Illegal OID");
222 else
223 snprint_objid(mystr, sizeof(mystr),
224 (oid *) oid1->name, oid1->len);
225 RETVAL = mystr;
226 }
227
228 OUTPUT:
229 RETVAL
230
231 void
232 nsop_to_array(oid1)
233 netsnmp_oid *oid1;
234 PREINIT:
235 int i;
236
237 PPCODE:
238 EXTEND(SP, (int)oid1->len);
239 for(i=0; i < (int)oid1->len; i++) {
240 PUSHs(sv_2mortal(newSVnv(oid1->name[i])));
241 }
242
243 SV *
244 nsop_get_indexes(oid1)
245 netsnmp_oid *oid1;
246 PREINIT:
247 int i, nodecount;
248 struct tree *tp, *tpe, *tpnode, *indexnode;
249 struct index_list *index;
250 netsnmp_variable_list vbdata;
251 char *buf = NULL;
252 size_t buf_len = 256, out_len = 0;
253 oid name[MAX_OID_LEN];
254 size_t name_len = MAX_OID_LEN;
255 oid *oidp;
256 size_t oidp_len;
257 AV *myret;
258 int is_private;
259
260 CODE:
261 {
262 memset(&vbdata, 0, sizeof(vbdata));
263 if (NULL == (tp = get_tree(oid1->name, oid1->len,
264 get_tree_head()))) {
265 RETVAL = NULL;
266 return;
267 }
268
269 if ((buf = netsnmp_malloc(buf_len)) == NULL) {
270 RETVAL = NULL;
271 return;
272 }
273
274 tpe = NULL;
275 nodecount = 0;
276 for(tpnode = tp; tpnode; tpnode = tpnode->parent) {
277 nodecount++;
278 if (nodecount == 2)
279 tpe = tpnode;
280 if (nodecount == 3 &&
281 (strlen(tpnode->label) < 6 ||
282 strcmp(tpnode->label + strlen(tpnode->label) - 5,
283 "Table"))) {
284 /* we're not within a table. bad logic, little choice */
285 netsnmp_free(buf);
286 RETVAL = NULL;
287 return;
288 }
289 }
290
291 if (!tpe) {
292 netsnmp_free(buf);
293 RETVAL = NULL;
294 return;
295 }
296
297 if (tpe->augments && strlen(tpe->augments) > 0) {
298 /* we're augmenting another table, so use that entry instead */
299 if (!snmp_parse_oid(tpe->augments, name, &name_len) ||
300 (NULL ==
301 (tpe = get_tree(name, name_len,
302 get_tree_head())))) {
303 netsnmp_free(buf);
304 RETVAL = NULL;
305 return; /* XXX: better error recovery needed? */
306 }
307 }
308
309 i = 0;
310 for(index = tpe->indexes; index; index = index->next) {
311 i++;
312 }
313
314 myret = (AV *) sv_2mortal((SV *) newAV());
315
316 oidp = oid1->name + nodecount;
317 oidp_len = oid1->len - nodecount;
318
319 for(index = tpe->indexes; index; index = index->next) {
320 /* XXX: NOT efficient! */
321 name_len = MAX_OID_LEN;
322 if (!snmp_parse_oid(index->ilabel, name, &name_len) ||
323 (NULL ==
324 (indexnode = get_tree(name, name_len,
325 get_tree_head())))) {
326 netsnmp_free(buf);
327 RETVAL = NULL;
328 return; /* xxx mem leak */
329 }
330 vbdata.type = mib_to_asn_type(indexnode->type);
331
332 if (vbdata.type == (u_char) -1) {
333 netsnmp_free(buf);
334 RETVAL = NULL;
335 return; /* XXX: not good. half populated stack? */
336 }
337
338 /* check for fixed length strings */
339 if (vbdata.type == ASN_OCTET_STR &&
340 indexnode->ranges && !indexnode->ranges->next
341 && indexnode->ranges->low == indexnode->ranges->high) {
342 vbdata.val_len = indexnode->ranges->high;
343 vbdata.type |= ASN_PRIVATE;
344 is_private = 1;
345 } else {
346 vbdata.val_len = 0;
347 if (index->isimplied) {
348 vbdata.type |= ASN_PRIVATE;
349 is_private = 1;
350 } else {
351 is_private = 0;
352 }
353 }
354
355 if (parse_one_oid_index(&oidp, &oidp_len, &vbdata, 0)
356 != SNMPERR_SUCCESS) {
357 netsnmp_free(buf);
358 RETVAL = NULL;
359 return;
360 }
361 out_len = 0;
362 if (is_private)
363 vbdata.type ^= ASN_PRIVATE;
364 out_len =
365 __snprint_value (buf, buf_len, &vbdata, indexnode,
366 vbdata.type, 0);
367 /*
368 sprint_realloc_value(&buf, &buf_len, &out_len,
369 1, name, name_len, &vbdata);
370 */
371 snmp_free_var_internals(&vbdata);
372 av_push(myret, newSVpv(buf, out_len));
373 }
374 netsnmp_free(buf);
375 RETVAL = newRV((SV *)myret);
376 }
377 OUTPUT:
378 RETVAL
379
380 void
381 nsop_append(oid1, string)
382 netsnmp_oid *oid1;
383 char *string;
384 PREINIT:
385 oid name[MAX_OID_LEN];
386 size_t name_len = MAX_OID_LEN;
387 int i;
388 CODE:
389 {
390 if (!snmp_parse_oid(string, (oid *) name, &name_len)) {
391 /* XXX */
392 }
393 if (oid1->len + name_len > MAX_OID_LEN) {
394 /* XXX: illegal */
395 }
396 for(i = 0; i < (int)name_len; i++) {
397 oid1->name[i+oid1->len] = name[i];
398 }
399 oid1->len += name_len;
400 }
401
402 void
403 nsop_append_oid(oid1, oid2)
404 netsnmp_oid *oid1;
405 netsnmp_oid *oid2;
406 PREINIT:
407 int i;
408 CODE:
409 {
410 if (oid1->len + oid2->len > MAX_OID_LEN) {
411 /* XXX: illegal */
412 }
413 for(i = 0; i < (int)oid2->len; i++) {
414 oid1->name[i+oid1->len] = oid2->name[i];
415 }
416 oid1->len += oid2->len;
417 }
418
419 int
420 nsop_length(oid1)
421 netsnmp_oid *oid1;
422 CODE:
423 {
424 RETVAL = oid1->len;
425 }
426 OUTPUT:
427 RETVAL
428
429 netsnmp_oid *
430 nsop_clone(oid1)
431 netsnmp_oid *oid1;
432 PREINIT:
433 netsnmp_oid *oid2;
434 CODE:
435 {
436 oid2 = nso_newarrayptr(oid1->name, oid1->len);
437 RETVAL = oid2;
438 }
439 OUTPUT:
440 RETVAL
441